2015-06-03 86 views
-1

我的問題是我想從自定義屬性中獲取數據,我不知道哪個arofutement給typeof()方法採取當前類型。反射不承認,該類是繼承的C#

[AttrN("test1")] 
[AttrC("test2")] 
public class cl2 :cl1 { 
    //should find attributes and set fields from base 
    public cl2() : base() { } 
} 

public class cl1 { 
    public string n; 
    public string c; 

    public cl1() { 
     setValuesFromCustomAttributes(); 
    } 
    private void setValuesFromCustomAttributes() { 
     foreach (var a in Attribute.GetCustomAttributes(typeof(cl1))) { 
      setn(a); 
      setc(a); 
     } 
    } 
    private void setn(Attribute attr) { 
     if (attr is AttrNAttribute) { 
      var x = (AttrNAttribute)attr; 
      n= x.getString; 
     } 
    } 
    private void setc(Attribute attr) { 
     if (attr is AttrCAttribute) { 
      var x = (AttrCAttribute)attr; 
      c = x.getString; 
     } 
    } 
} 
+2

'public class cl2:cl2'? –

+0

class cl2從cl1繼承fixed –

+0

https://msdn.microsoft.com/en-us/library/dwc6ew1d(v=vs.110).aspx – TyCobb

回答

0

只是改變這一行:

的foreach(VAR一個在Attribute.GetCustomAttributes(typeof運算(CL1)))

的foreach(VAR一個在Attribute.GetCustomAttributes (this.GetType()))