C#使用带有Delegate.CreateDelegate的值类型的属性 [英] C# using properties with value types with Delegate.CreateDelegate

查看:2831
本文介绍了C#使用带有Delegate.CreateDelegate的值类型的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Jon Skeet的文章反思飞行和探索代表作为指导,我试图使用Delegate.CreateDelegate方法来复制属性作为委托。以下是一个示例类:

  public class PropertyGetter 
{
public int Prop1 {get; set;}
public string Prop2 {get; set;}

public object GetPropValue(string propertyName)
{
var property = GetType()。GetProperty(propertyName).GetGetMethod ();
propertyDelegate =(Func< object>)Delegate.CreateDelegate(typeof(Func< object>)),this,property);

return propertyDelegate();
}
}

我遇到的问题是当我打电话给 GetPropValue 并传入Prop1作为参数,我得到一个 ArgumentException 调用 Delegate.CreateDelegate 与消息无法绑定到目标方法,因为它的签名或安全透明度不兼容的代理类型。当使用返回包含结构体的原始/值类型的任何属性时,会发生这种情况。



有人知道一种方式能够在这里使用引用和值类型?

解决方案

从根本上说,你的一般方法是不可能的。您可以采取所有非价值类型并将其视为 Func< object> 的原因是依赖于违规( Func< T> ; 相对于 T 是相反的)。根据语言规范,违反不支持价值类型。



当然,如果您不依赖于使用该方法,问题更容易。 >

如果您只想使用 PropertyInfo.GetValue 方法获取该值:

  public object GetPropValue(string name)
{
返回GetType()。GetProperty(name).GetValue(this);
}

如果要返回一个 Func< object> 它会在调用时获取值,只需在该反射调用中创建一个lambda:

  public Func< object> GetPropValue2(string name)
{
return()=> GetType()。GetProperty(name).GetValue(this);
}


Using Jon Skeet's article Making reflection fly and exploring delegates as a guide, I am trying to use the Delegate.CreateDelegate method to duplicate properties as delegates. Here's an example class:

public class PropertyGetter
{
    public int Prop1 {get;set;}
    public string Prop2 {get;set;}

    public object GetPropValue(string propertyName)
    {
        var property = GetType().GetProperty(propertyName).GetGetMethod();
        propertyDelegate = (Func<object>)Delegate.CreateDelegate(typeof(Func<object>), this, property);

        return propertyDelegate();
    }
}

The problem I'm having is when I call GetPropValue and pass in "Prop1" as the parameter, I get an ArgumentException on the call to Delegate.CreateDelegate with the message "Cannot bind to the target method because its signature or security transparency is not compatible with that of the delegate type." This happens when using any property that returns a primitive/value type including structs.

Does anybody know a way to be able to use both reference and value types here?

解决方案

Fundamentally your general approach isn't possible. The reason you're able to take all non-value types and treat them as a Func<object> is by relying on contravariance (Func<T> is contravariant with respect to T). As per the language specs, contravariance does not support value types.

Of course, the problem is easier if you just don't rely on using that approach.

If you just want to get the value use the PropertyInfo.GetValue method:

public object GetPropValue(string name)
{
    return GetType().GetProperty(name).GetValue(this);
}

If you want to return a Func<object> which will fetch the value whenever it's called, just create a lambda around that reflection call:

public Func<object> GetPropValue2(string name)
{
    return () => GetType().GetProperty(name).GetValue(this);
}

这篇关于C#使用带有Delegate.CreateDelegate的值类型的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆