是否有可能通过反思得到一个房产的私人制定者?

我写了一个自定义序列化程序,通过reflection设置对象属性。 可序列化类用serializable属性标记,所有可序列化属性也被标记。 例如,以下类是可序列化的:

[Serializable] public class Foo { [SerializableProperty] public string SomethingSerializable {get; set;} public string SometthingNotSerializable {get; set;} } 

当系列化程序被要求反序列化SomethingSerializable ,它获取属性的set方法并使用它来设置它,如下所示:

 PropertyInfo propertyInfo; //the property info of the property to set //...// if (propertyInfo.CanWrite && propertyInfo.GetSetMethod() != null) { propertyInfo.GetSetMethod().Invoke(obj, new object[]{val}); } 

这工作正常,但是,如何才能使属性设置器只对序列化器可访问? 如果setter是私有的:

 public string SomethingSerializable {get; private set;} 

然后对propertyInfo.GetSetMethod()的调用在序列化程序中返回null。 有没有办法访问私有的setter或任何其他方式,以确保只有序列化程序可以访问setter? 不保证序列化程序在同一个程序集中。

正如您已经想到的那样,访问非公共setter的一种方法如下:

 PropertyInfo property = typeof(Type).GetProperty("Property"); property.DeclaringType.GetProperty("Property"); property.GetSetMethod(true).Invoke(obj, new object[] { value }); 

不过还有另外一种方法:

 PropertyInfo property = typeof(Type).GetProperty("Property"); property.DeclaringType.GetProperty("Property"); property.SetValue(obj, value, BindingFlags.NonPublic | BindingFlags.Instance, null, null, null); // If the setter might be public, add the BindingFlags.Public flag. 

来自搜索引擎?

这个问题具体是关于访问公共财产中的非公共二传手。

  • 如果属性和setter都是公共的,则只有第一个示例适合您。 要使第二个示例起作用,您需要添加BindingFlags.Public标志。
  • 如果属性是在父类型中声明的,并且对于您调用GetProperty的类型不可见,则您将无法访问它。 您需要在属性可见的类型上调用GetProperty 。 (只要属性本身可见,这不会影响私有的setter。)
  • 如果inheritance链中有相同属性的多个声明(通过new关键字),这些示例将针对调用GetProperty的类型立即可见的属性。 例如,如果类A使用public int Property声明Property,而类B通过public new int Property重新声明Property,则typeof(B).GetProperty("Property")将返回在B中声明的属性,而typeof(A).GetProperty("Property")将返回在A中声明的属性。