asg*_*las 1 c# propertydescriptor typedescriptor
我正在尝试从类型中获取所有属性,但是使用TypeDescriptor.GetProperties(thisType)只会为我提供属性,它具有setter和getter.我有只写属性.有没有办法检索PropertyDescriptorCollection,包括那些?
/导演Asger
只写属性是一种罕见的野兽,并且在System.ComponentModel/PropertyDescriptor空间中不存在.PropertyDescriptors旨在可读.我可能会破解HyperDescriptor只写只写属性,但这将是一个黑客 - 它可能会抛出异常get,这可能会影响调用代码相当多.
作为旁白; 我通常建议不要使用只写属性; 人们小跑出来的教科书示例是密码(public string Password {private get;set;}) - 我宁愿有一个void SetPassword(string newPassword)方法......
你真的想做什么?这里有一系列选项,都可以实现:
Delegate.CreateDelegate(非常容易)Expression.Compile(一个有点困难,但不是很多)Reflection.Emit(相当难)PropertyDescriptor(非常难)如果你告诉我你真正想做的事情(而不是你目前正在尝试做的方式),我可能会提供更多帮助.
作为一个使用示例Delegate.CreateDelegate(请注意,您希望将代理存储在某处并重复使用它很多次):
编辑以显示如果在运行时不知道特定类型,如何执行此操作
using System;
using System.Reflection;
class Foo
{
public string Bar { private get; set; }
public override string ToString()
{
return Bar; // to prove working
}
}
static class Program
{
static void Main()
{
ISetter setter = Setter.Create(typeof(Foo), "Bar");
Foo foo = new Foo();
setter.SetValue(foo, "abc");
string s = foo.ToString(); // prove working
}
}
public interface ISetter {
void SetValue(object target, object value);
}
public static class Setter
{
public static ISetter Create(Type type, string propertyName)
{
if (type == null) throw new ArgumentNullException("type");
if (propertyName == null) throw new ArgumentNullException("propertyName");
return Create(type.GetProperty(propertyName));
}
public static ISetter Create(PropertyInfo property)
{
if(property == null) throw new ArgumentNullException("property");
if (!property.CanWrite) throw new InvalidOperationException("Property cannot be written");
Type type = typeof(TypedSetter<,>).MakeGenericType(
property.ReflectedType, property.PropertyType);
return (ISetter) Activator.CreateInstance(
type, property.GetSetMethod());
}
}
public class TypedSetter<TTarget, TValue> : ISetter {
private readonly Action<TTarget, TValue> setter;
public TypedSetter(MethodInfo method) {
setter = (Action<TTarget, TValue>)Delegate.CreateDelegate(
typeof(Action<TTarget, TValue>), method);
}
void ISetter.SetValue(object target, object value) {
setter((TTarget)target, (TValue)value);
}
public void SetValue(TTarget target, TValue value) {
setter(target, value);
}
}
Run Code Online (Sandbox Code Playgroud)
或者使用ExpressionAPI(.NET 3.5):
using System;
using System.Linq.Expressions;
using System.Reflection;
class Foo
{
public string Bar { private get; set; }
public override string ToString()
{
return Bar; // to prove working
}
}
static class Program
{
static void Main()
{
Action<object,object> setter = Setter.Create(typeof(Foo), "Bar");
Foo foo = new Foo();
setter(foo, "abc");
string s = foo.ToString();
}
}
public static class Setter
{
public static Action<object,object> Create(Type type, string propertyName)
{
if (type == null) throw new ArgumentNullException("type");
if (propertyName == null) throw new ArgumentNullException("propertyName");
return Create(type.GetProperty(propertyName));
}
public static Action<object,object> Create(PropertyInfo property)
{
if(property == null) throw new ArgumentNullException("property");
if (!property.CanWrite) throw new InvalidOperationException("Property cannot be written");
var objParam = Expression.Parameter(typeof(object), "obj");
var valueParam = Expression.Parameter(typeof(object), "value");
var body = Expression.Call(
Expression.Convert(objParam, property.ReflectedType),
property.GetSetMethod(),
Expression.Convert(valueParam, property.PropertyType));
return Expression.Lambda<Action<object, object>>(
body, objParam, valueParam).Compile();
}
}
Run Code Online (Sandbox Code Playgroud)