如何为 .NET 属性创建委托? [英] How do I create a delegate for a .NET property?

查看:25
本文介绍了如何为 .NET 属性创建委托?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为以下对象创建委托(作为测试):

I am trying to create a delegate (as a test) for:

Public Overridable ReadOnly Property PropertyName() As String

我的直觉尝试是像这样声明委托:

My intuitive attempt was declaring the delegate like this:

Public Delegate Function Test() As String

并像这样实例化:

Dim t As Test = AddressOf e.PropertyName

但这会引发错误:

方法'公共可覆盖只读属性PropertyName() AsString' 没有签名与委托 'Delegate 兼容函数 Test() As String'.

Method 'Public Overridable ReadOnly Property PropertyName() As String' does not have a signature compatible with delegate 'Delegate Function Test() As String'.

所以因为我正在处理一个属性,所以我尝试了这个:

So because I was dealing with a property I tried this:

Public Delegate Property Test() As String

但这会引发编译器错误.

But this throws a compiler error.

那么问题来了,我如何为一个属性创建一个委托?

So the question is, how do I make a delegate for a property?

查看此链接:

http://peisker.net/dotnet/propertydelegates.htm

推荐答案

重新使用 AddressOf 的问题 - 如果您在编译时知道 prop-name,您可以(至少在 C# 中)使用匿名方法/lambda:

Re the problem using AddressOf - if you know the prop-name at compile time, you can (in C#, at least) use an anon-method / lambda:

Test t = delegate { return e.PropertyName; }; // C# 2.0
Test t = () => e.PropertyName; // C# 3.0

我不是 VB 专家,但反射器声称这与以下内容相同:

I'm not a VB expert, but reflector claims this is the same as:

Dim t As Test = Function 
    Return e.PropertyName
End Function

那行得通吗?

原答案:

您可以使用 Delegate.CreateDelegate 为属性创建委托;这可以为该类型的任何实例打开,对于单个实例固定 - 并且可以用于 getter 或 setter;我将在 C# 中举一个例子...

You create delegates for properties with Delegate.CreateDelegate; this can be open for any instance of the type, of fixed for a single instance - and can be for getter or setter; I'll give an example in C#...

using System;
using System.Reflection;
class Foo
{
    public string Bar { get; set; }
}
class Program
{
    static void Main()
    {
        PropertyInfo prop = typeof(Foo).GetProperty("Bar");
        Foo foo = new Foo();

        // create an open "getter" delegate
        Func<Foo, string> getForAnyFoo = (Func<Foo, string>)
            Delegate.CreateDelegate(typeof(Func<Foo, string>), null,
                prop.GetGetMethod());

        Func<string> getForFixedFoo = (Func<string>)
            Delegate.CreateDelegate(typeof(Func<string>), foo,
                prop.GetGetMethod());

        Action<Foo,string> setForAnyFoo = (Action<Foo,string>)
            Delegate.CreateDelegate(typeof(Action<Foo, string>), null,
                prop.GetSetMethod());

        Action<string> setForFixedFoo = (Action<string>)
            Delegate.CreateDelegate(typeof(Action<string>), foo,
                prop.GetSetMethod());

        setForAnyFoo(foo, "abc");
        Console.WriteLine(getForAnyFoo(foo));
        setForFixedFoo("def");
        Console.WriteLine(getForFixedFoo());
    }
}

这篇关于如何为 .NET 属性创建委托?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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