c#类内部setter和PropertyGrid
我有Winform应用程序,表单上有PropertyGrid。Person类具有内部setter的属性(年龄)。为什么PropertyGrid上的属性是只读的?从逻辑上讲,PropertyGrid是同一部件的成员,部件成员可以访问内部构件c#类内部setter和PropertyGrid,c#,propertygrid,internal,access-modifiers,C#,Propertygrid,Internal,Access Modifiers,我有Winform应用程序,表单上有PropertyGrid。Person类具有内部setter的属性(年龄)。为什么PropertyGrid上的属性是只读的?从逻辑上讲,PropertyGrid是同一部件的成员,部件成员可以访问内部构件 public class Person { public string Name { get; set; } internal int Bio { get; set; } private int age; public in
public class Person
{
public string Name { get; set; }
internal int Bio { get; set; }
private int age;
public int Age
{
get { return age; }
internal set { age = value; }
}
public string Location { get; internal set; }
}
谢谢。PropertyGrid位于它自己的程序集(
System.Windows.Forms.dll
)中,独立于您的程序集
因此,它无权访问在程序集中指定为内部的属性、方法或字段
仅从定义Person
的同一程序集中引用Person
和PropertyGrid
,将不会使Age
setter在System.Windows.Forms.dll
中可见
另外,PropertyGrid
很可能使用与以下类似的反射:
object obj = this.SelectedObject;
Type type = obj.GetType();
PropertyInfo[] props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach(PropertyInfo pInfo in props)
{
if (pInfo.CanWrite)
{
// mark as read/write
}
else
{
// mark as read-only
}
}
仅检索
public
属性。将返回Age
prpperty,但标记为CanWrite==false
,您的意思是没有任何解决方法?谢谢