カスタム プロパティに問題があります。これは問題のプロパティです:
[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 (...);
私の属性は、そのターゲットとそのプロパティを正しく設定しました。null または設定されていないものはありません。しかし、コードの他の場所でそのプロパティを取得しようとすると、設定したことがないかのようにすべてが null に戻ります...
クラスの属性は、PropertyInfo から動的に取得したときにインスタンス化されますか、またはプロパティに物理インスタンスが設定されていますか?
なにが問題ですか?