Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 派生类无法使用PropertyDescriptorCollection.SetValue访问受保护的setter_C#_Reflection_Typedescriptor_Propertydescriptor - Fatal编程技术网

C# 派生类无法使用PropertyDescriptorCollection.SetValue访问受保护的setter

C# 派生类无法使用PropertyDescriptorCollection.SetValue访问受保护的setter,c#,reflection,typedescriptor,propertydescriptor,C#,Reflection,Typedescriptor,Propertydescriptor,我已将基类中的一个属性设置为具有受保护的setter。 这很好,我可以在派生类的构造函数中设置属性-但是,当我尝试使用PropertyDescriptorCollection设置此属性时,它将不会设置,但是使用集合可以处理所有其他属性 我应该提到的是,当我重新移动受保护的访问修饰符时,所有的修饰符都可以正常工作……但当然,现在它没有受到保护。谢谢你的意见 class base_a { public string ID { get; protected set; } public vir

我已将基类中的一个属性设置为具有受保护的setter。 这很好,我可以在派生类的构造函数中设置属性-但是,当我尝试使用PropertyDescriptorCollection设置此属性时,它将不会设置,但是使用集合可以处理所有其他属性

我应该提到的是,当我重新移动受保护的访问修饰符时,所有的修饰符都可以正常工作……但当然,现在它没有受到保护。谢谢你的意见

 class base_a
{

 public  string ID { get; protected set; }
 public virtual void SetProperties(string xml){}
}

class derived_a : base_a
 {
   public derived_a()
    {
    //this works fine
     ID = "abc"
    }
   public override void SetProperties(string xml)
    {
      PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(this);
      //this does not work...no value set.
      pdc["ID"].SetValue(this, "abc");

      }
  }

TypeDescriptor
不知道您从应该可以访问该属性设置器的类型调用它,因此您使用的
PropertyDescriptor
是只读的(您可以通过检查来验证这一点)。当您尝试设置只读
属性描述符的值时,什么也不会发生

要解决此问题,请使用法线反射:

var property = typeof(base_a).GetProperty("ID");

property.SetValue(this, "abc", null);

TypeDescriptor
不知道您从应该可以访问该属性设置器的类型调用它,因此您使用的
PropertyDescriptor
是只读的(您可以通过检查来验证这一点)。当您尝试设置只读
属性描述符的值时,什么也不会发生

要解决此问题,请使用法线反射:

var property = typeof(base_a).GetProperty("ID");

property.SetValue(this, "abc", null);
试试这个

PropertyInfo[] props = TypeDescriptor
    .GetReflectionType(this)
    .GetProperties();
props[0].SetValue(this, "abc", null);
或者干脆

PropertyInfo[] props = this
    .GetType()
    .GetProperties();
props[0].SetValue(this, "abc", null);
(您需要使用System.Reflection;
)试试这个

PropertyInfo[] props = TypeDescriptor
    .GetReflectionType(this)
    .GetProperties();
props[0].SetValue(this, "abc", null);
或者干脆

PropertyInfo[] props = this
    .GetType()
    .GetProperties();
props[0].SetValue(this, "abc", null);

(您需要一个
使用System.Reflection;

您说的“不工作”是什么意思?它做什么?svick,我检查属性时没有设置值。没有引发异常。我假设这是因为集合受到保护,但是我在VS帮助中找不到任何说明这种情况的内容。我也不喜欢它在没有警告或异常的情况下什么都不做。@JoelRondeau,这段代码怎么可能产生警告呢。一个例外可能会有一些意义。@svick-我意识到,在这种情况下,通过反思,不可能出现警告。如果你能做同样的事情而不进行反射,你会得到一个编译器错误。你说“不工作”是什么意思?它做什么?svick,我检查属性时没有设置值。没有引发异常。我假设这是因为集合受到保护,但是我在VS帮助中找不到任何说明这种情况的内容。我也不喜欢它在没有警告或异常的情况下什么都不做。@JoelRondeau,这段代码怎么可能产生警告呢。一个例外可能会有一些意义。@svick-我意识到,在这种情况下,通过反思,不可能出现警告。只是如果你能做同样的事情而不进行反射,你会得到一个编译器错误。谢谢,它成功了。正在设置的某些属性来自派生类。我检查了GetProperty是否返回了一个null值,作为该道具是否来自基类的指示符……想法?是的,
GetProperty()
在这些情况下应该返回
null
。但是,如果您也想从派生类中获得属性,只需使用
typeof(derived\u a)
。谢谢,它很管用。正在设置的某些属性来自派生类。我检查了GetProperty是否返回了一个null值,作为该道具是否来自基类的指示符……想法?是的,
GetProperty()
在这些情况下应该返回
null
。但是,如果您也希望从派生类获得属性,只需使用
typeof(派生的)