2011-05-24 93 views
3

我有一個接口:C#反射獲取具體類的靜態屬性從接口

interface IInterface 
{ 
    string Name { get; } 
} 

由一個通用的抽象類來實現:

public class BInterface<T> : IInterface 
{ 
    static BInterface() 
    { 
     // Or anything that would be implementation class specific 
     Name = typeof(BInterface<>).GetType().Name; 
    } 
    public static string Name { get; private set; } 
    string IInterface.Name { get { return Name; } } 
} 

後者又在具體的類實現:

public class CInterface : BInterface<int> 
{ 
} 

我知道如何通過'type.IsAssignableFrom'獲得對具體類的引用, '!type.IsInterface'和'!type.IsAbstract',但是就我所管理的而言。

我需要通過反射來獲取任何具體類的靜態名稱屬性的值。但是,對於我可憐的大腦的生活,我無法確定這些代碼。任何提示都會很棒。

EDIT(在澄清):

我知道,靜態屬性需要從基類閱讀。但是......

靜態字段將包含具體類的基本名稱 - >通過基類的靜態構造函數中的反射派生。這是有效的(我知道如何完成它),因爲我們在整個地方都做到了。

我這種情況下,我試圖建立一個工廠類,需要知道這個靜態字段,並需要通過反射到達它由於工廠實現的一些(其他)要求。

EDIT(再次)擴展代碼:

這裏就是我試圖完成一個幾乎完整的,如果沒用的,例如。

public interface IInterface 
    { 
     string Name { get; } 
     object Value { get; set; } 
    } 

    public class BInterface<T> : IInterface 
    { 
     static BInterface() 
     { 
      // Or anything that would be implementation class specific 
      Name = typeof(BInterface<>).GetType().Name; // Should be CInterface, DInterface depending on which class it is called from. 
     } 

    string IInterface.Name { get { return Name; } } 
    object IInterface.Value { get { return Value; } set { Value = (T)value; } } 

    public static string Name { get; private set; } 
    public T Value { get; set; } 
} 

public class CInterface : BInterface<int> 
{ 
} 

public class DInterface : BInterface<double> 
{ 
} 

public static class InterfaceFactory 
{ 
    private readonly static IDictionary<string, Type> InterfaceClasses; 

    static InterfaceFactory() 
    { 
     InterfaceClasses = new Dictionary<string, Type>(); 

     var assembly = Assembly.GetExecutingAssembly(); 
     var interfaceTypes = assembly.GetTypes() 
            .Where(type => type.IsAssignableFrom(typeof (IInterface)) 
             && !type.IsInterface 
             && !type.IsAbstract); 
     foreach (var type in interfaceTypes) 
     { 
      // Get name somehow 
      var name = "..."; 
      InterfaceClasses.Add(name, type); 
     } 
    } 

    public static IInterface Create(string key, object value, params object[] parameters) 
    { 
     if (InterfaceClasses.ContainsKey(key)) 
     { 
      var instance = (IInterface) Activator.CreateInstance(InterfaceClasses[key], parameters); 
      instance.Value = value; 

      return instance; 
     } 
     return null; 
    } 
} 

在foreach循環內的IntefaceFactory的靜態構造函數的部分是什麼,我試圖解決的問題。希望這個更清楚。

+0

爲什麼一定要通過反射?你的實現類明確地返回'static'屬性作爲它的實現。因此,任何具體的類實例都將通過使用接口返回該'static'屬性。 – pickypg 2011-05-24 01:41:41

+0

@pickypg:這是一個缺少問題的觀點。 OP顯然認爲每個繼承的成員都會得到自己的「static」成員「Name」的「類實例」,每個類都會有所不同。當然,情況並非如此。 – jason 2011-05-24 01:42:51

+0

@Jason我有點同意,但我認爲整個願望要麼是誤導或作業。 – pickypg 2011-05-24 01:46:08

回答

3

這是如何從實例中得到具體類的靜態屬性:

var staticProperty = instance.GetType() 
    .GetProperty("<PropertyName>", BindingFlags.Public | BindingFlags.Static); 
var value = staticProperty.GetValue(instance, null); 
+0

@Jason:但這個問題沒有意義,所以... – 2011-05-24 01:58:44

+0

@Ed S - 我試圖找到一些。至少我在問題標題中找到了自己的意思,並且確實標題提出了什麼問題。 – 2011-05-24 02:01:41

+0

@Ed S .:我同意你的意見。 – jason 2011-05-24 13:20:31

1

static成員不按照您的想法工作。它們屬於基類,因此您嘗試使用繼承的成員是不可能的。