简体   繁体   English

公共访问者

[英]Public accessors

I have the following accessor in Glass class: 我在Glass类中有以下访问器:

public bool isGlassEnabled{
    get {
         if (System.Environment.OSVersion.Version.Major >= 6) {
             DwmIsCompositionEnabled( ref en );
             if (en > 0) {
                return true;
             } else { return false; }
         } else { return false; }
    }
}

(I am not sure if the code is working, but that is not what I am interested in) (我不确定代码是否有效,但这不是我感兴趣的)

Now we are located in MainClass . 现在我们位于MainClass Is there any possibility to access isGlassEnabled like this: 是否有可能像这样访问isGlassEnabled

bool myBool = **without new** Glass.isGlassEnabled;

To make this work 为了使这项工作

bool myBool = **without new** Glass.isGlassEnabled;

you just need a static property: 您只需要一个static属性:

public static bool isGlassEnabled{
    get { ... }
}

And then bool myBool = Glass.isGlassEnabled; 然后bool myBool = Glass.isGlassEnabled; will simply compile. 只会编译。

Everything you (might) need in the getter is static as well so no problem there. 您(可能)在吸气剂中需要的所有内容也是静态的,因此没有问题。

Edit: 编辑:

as noted by others, your code contains a variable en that should be either local or static. 正如其他人所指出的那样,您的代码包含应为本地或静态的变量en
Together with some else-branch folding: 连同其他分支折叠:

public static bool isGlassEnabled
{
    get 
    {
         if (System.Environment.OSVersion.Version.Major >= 6) 
         {
             Int32 en;   // or whatever type exactly needed
             DwmIsCompositionEnabled( ref en );
             if (en > 0) 
                return true;                
         }
         return false; 
    }
}

是的,您应该将property标记为static

public static bool isGlassEnabled

使用静态修饰符

public static bool isGlassEnabled

Use static properties: 使用静态属性:

public static bool isGlassEnabled{
    get {
        TypeTheTypeOfEnVariableHere en;
        if (System.Environment.OSVersion.Version.Major >= 6) {
             DwmIsCompositionEnabled( ref en );
            if (en > 0) {
               return true;
            } else { return false; }
        } else { return false; }
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM