草庐IT

anonymous-delegates

全部标签

c# - 通用接口(interface)方法的开放委托(delegate)

我正在尝试创建一个openinstancedelegate对于通用接口(interface)方法,但我一直收到NotSupportedException。这是不会运行的简化代码:interfaceIFoo{voidBar(Tj);}classFoo:IFoo{publicvoidBar(Tj){}}staticvoidMain(string[]args){varbar=typeof(IFoo).GetMethod("Bar").MakeGenericMethod(typeof(int));varx=Delegate.CreateDelegate(typeof(Action),null,

c# - 通用接口(interface)方法的开放委托(delegate)

我正在尝试创建一个openinstancedelegate对于通用接口(interface)方法,但我一直收到NotSupportedException。这是不会运行的简化代码:interfaceIFoo{voidBar(Tj);}classFoo:IFoo{publicvoidBar(Tj){}}staticvoidMain(string[]args){varbar=typeof(IFoo).GetMethod("Bar").MakeGenericMethod(typeof(int));varx=Delegate.CreateDelegate(typeof(Action),null,

c# - 为事件定义一个空的委托(delegate)主体是一个好习惯吗?

这个问题在这里已经有了答案:关闭11年前。PossibleDuplicate:Isthereadownsidetoaddingananonymousemptydelegateoneventdeclaration?为事件定义一个空的委托(delegate)主体是否是一种好习惯,这样您就不必担心引发没有事件处理程序的事件?(无需检查事件是否为空)。喜欢下面的代码:publiceventEventHandlerLoadedData=delegate{};

c# - 为事件定义一个空的委托(delegate)主体是一个好习惯吗?

这个问题在这里已经有了答案:关闭11年前。PossibleDuplicate:Isthereadownsidetoaddingananonymousemptydelegateoneventdeclaration?为事件定义一个空的委托(delegate)主体是否是一种好习惯,这样您就不必担心引发没有事件处理程序的事件?(无需检查事件是否为空)。喜欢下面的代码:publiceventEventHandlerLoadedData=delegate{};

c# - 为什么不能将 .NET 委托(delegate)声明为静态的?

当我尝试编译以下内容时:publicstaticdelegatevoidMoveDelegate(Actorsender,MoveDirectionargs);我收到错误消息:“修饰符‘static’对此项无效。”我在单例中实现了这个,有一个单独的类调用委托(delegate)。问题是,当我在另一个类中使用单例实例调用委托(delegate)(从标识符,而不是类型)时,无论出于何种原因,我都不能这样做,即使我声明委托(delegate)是非静态的。显然,当且仅当委托(delegate)是静态的时,我才能直接通过类型引用它。这背后的原因是什么?我正在使用MonoDevelop2.4.2。

c# - 为什么不能将 .NET 委托(delegate)声明为静态的?

当我尝试编译以下内容时:publicstaticdelegatevoidMoveDelegate(Actorsender,MoveDirectionargs);我收到错误消息:“修饰符‘static’对此项无效。”我在单例中实现了这个,有一个单独的类调用委托(delegate)。问题是,当我在另一个类中使用单例实例调用委托(delegate)(从标识符,而不是类型)时,无论出于何种原因,我都不能这样做,即使我声明委托(delegate)是非静态的。显然,当且仅当委托(delegate)是静态的时,我才能直接通过类型引用它。这背后的原因是什么?我正在使用MonoDevelop2.4.2。

c# - 反射性能 - 创建委托(delegate)(属性 C#)

我在使用反射时遇到了性能问题。所以我决定为我的对象的属性创建委托(delegate),到目前为止得到了这个:TestClasscwp=newTestClass();varpropertyInt=typeof(TestClass).GetProperties().Single(obj=>obj.Name=="AnyValue");varaccess=BuildGetAccessor(propertyInt.GetGetMethod());varresult=access(cwp);staticFuncBuildGetAccessor(MethodInfomethod){varobj=Ex

c# - 反射性能 - 创建委托(delegate)(属性 C#)

我在使用反射时遇到了性能问题。所以我决定为我的对象的属性创建委托(delegate),到目前为止得到了这个:TestClasscwp=newTestClass();varpropertyInt=typeof(TestClass).GetProperties().Single(obj=>obj.Name=="AnyValue");varaccess=BuildGetAccessor(propertyInt.GetGetMethod());varresult=access(cwp);staticFuncBuildGetAccessor(MethodInfomethod){varobj=Ex

c# - 具有零参数的 Action 委托(delegate)

我在许多使用Action委托(delegate)的在线示例中看到了这一行:publiceventActionMyEvent;但是当我在自己的代码中尝试时,我得到了这个错误Usingthegenerictype'System.Action'requires'1'typearguments文档当然描述了一种没有任何类型参数的Action形式。我错过了什么? 最佳答案 扩展安德鲁斯的回答。在非3.5场景中使用Action是完全合法的。只需自己定义即可。publicdelegatevoidAction();

c# - 具有零参数的 Action 委托(delegate)

我在许多使用Action委托(delegate)的在线示例中看到了这一行:publiceventActionMyEvent;但是当我在自己的代码中尝试时,我得到了这个错误Usingthegenerictype'System.Action'requires'1'typearguments文档当然描述了一种没有任何类型参数的Action形式。我错过了什么? 最佳答案 扩展安德鲁斯的回答。在非3.5场景中使用Action是完全合法的。只需自己定义即可。publicdelegatevoidAction();