如何为.NET属性创建委托? [英] How do I create a delegate for a .NET property?
问题描述
我正在尝试创建一个委托(作为测试):
公共可覆盖的ReadOnly属性PropertyName()As String
我直观的尝试是这样宣布委托:
公共委托函数Test()As String
并实例化如下:
Dim t As Test = AddressOf e.PropertyName
/ pre>
但是这会抛出错误:
方法'公共覆盖ReadOnly属性PropertyName()As
String'没有签名
与委托兼容代理
函数Test()As String'。
所以因为我正在处理一个财产我试过这个:
公共委托属性测试()As String
但是这会抛出编译器错误。
所以问题是,我该怎么办? a。一个财产的代表?
看到这个链接:
http://peisker.net/dotnet/propertydelegates.htm
解决方案使用AddressOf重新解决问题 - 如果您在编译时知道prop-name,可以(至少在C#中)使用anon-方法/ lambda:
Test t = delegate {return e.PropertyName; }; // C#2.0
Test t =()=> e.PropertyName; // C#3.0
我不是VB专家,反射器声称这是一样的:
Dim t As Test = Function
返回e.PropertyName
结束函数
这是否有效?
原始答案:
您为
Delegate.CreateDelegate
的属性创建代理;这可以为任何类型的实例打开,对于单个实例是固定的 - 可以用于getter或setter;我将在C#中给出一个例子。using System;
使用System.Reflection;
class Foo
{
public string Bar {get;组;
}
类程序
{
static void Main()
{
PropertyInfo prop = typeof(Foo).GetProperty(Bar);
Foo foo = new Foo();
//创建一个打开的getter委托
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());
动作< Foo,string> setForAnyFoo =(Action< Foo,string>)
Delegate.CreateDelegate(typeof(Action< Foo,string>),null,
prop.GetSetMethod());
动作< string> setForFixedFoo =(Action< string>)
Delegate.CreateDelegate(typeof(Action< string>),foo,
prop.GetSetMethod());
setForAnyFoo(foo,abc);
Console.WriteLine(getForAnyFoo(foo));
setForFixedFoo(def);
Console.WriteLine(getForFixedFoo());
}
}
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
And instantiating like this:
Dim t As Test = AddressOf e.PropertyName
But this throws the error:
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?
See this link:
http://peisker.net/dotnet/propertydelegates.htm
解决方案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
I'm not a VB expert, but reflector claims this is the same as:
Dim t As Test = Function Return e.PropertyName End Function
Does that work?
Original answer:
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屋!