0
我有一個自定義屬性的問題。 這是有問題的性質:自定義屬性內部屬性null c#
[AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
public class UIProperty : Attribute
{
public UIProperty(string property, Type target) { Property = property; TargetType = target; }
private string property;
public string Property { get{return property;} set{property = value;} }
private PropertyInfo targetProperty;
public PropertyInfo TargetProperty { get { return targetProperty; } protected set { targetProperty = value; } }
private Type targetType;
public Type TargetType { get{ return targetType; } set{ targetType = value; } }
private object target; public object Target { get{return target; } set{ target = value;} }
public void SetTargetProperty(PropertyInfo targetPropertyInfo, object target)
{
targetProperty = targetPropertyInfo;
this.target = target;
}
public object TargetValue
{
get { return targetProperty.GetValue(target, null); }
set { if (!value.Equals(TargetValue)) targetProperty.SetValue(target, value, null); }
}
}
所以,如果我用這種方式取出後,通過設置SetTargetProperty自定義屬性:
UIProperty uip = this.GetType(). GetProperty ("name"). GetCustomAttributes (typeof (UIProperty), true) [0] as UIProperty;
uip.SetTargetProperty (...);
我的屬性已正確設置它的目標和它的屬性。 什麼都不爲空或未設置。 但是,當我去代碼中的其他地方拿起屬性時,一切都回到了空,就好像我從未設置過一樣...
當我從PropertyInfo動態獲取或者它們有一個物理實例設置爲屬性?
出了什麼問題?