简体   繁体   English

c#从类的实例中获取静态属性的值

[英]c# Get value of static property from the instance of a class

How do I get the value of a static property from an instance of a class?如何从类的实例中获取静态属性的值? see example below...见下面的例子...

abstract class A {
   public static double Foo {get; protected set;}
}

class B : A {
   static B(){
      Foo = 1;
   }
}

class C : A {
   static C(){
      Food = 2;
   }
}

class Test {
   A test = new B();

   //How do I get test.Foo ??
}

Static members (fields, properties, methods, etc.) are accessed via class name.静态成员(字段、属性、方法等)通过类名访问。

var x = A.Foo;

I think you are getting statics wrong.我认为你弄错了静力学。

Did you mean something like this?你的意思是这样的吗?

abstract class A {
   public double Foo {get; protected set;}
}

class B : A {
   public B(){
      Foo = 1;
   }
}

class C : A {
   public C(){
      Foo = 2;
   }
}

class Test {
   A test = new B();
   var foo = test.Foo;
}

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

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