当前位置: 首页 > 编程语言 > C#

使用策略模式共享在C#上实现多参数C++模板行为

时间:2023-04-10 20:05:30 C#

使用策略模式在C#上实现多参数C++模板行为模板这里是模式的一个例子:interfaceISomePolicy{void_doSomething(Uu);}classMyClass:ISomePolicy,ISomePolicy{internalTmyElement{get;set;}publicMyClass(TElement){myElement=Element;}voidISomePolicy._doSomething(doubleu){Console.WriteLine("thisisint,double");}voidISomePolicy._doSomething(intu){Console.WriteLine("这是int,int");}}staticclassMyClassExtension{//我想做什么我的预期行为是这样的:MyClassoClass=newMyClass(3);oClass.doSomething(0.5);//这有效oClass.doSomething(1);//这有效oClass.doSomething("Thisshouldfail");//编译时中断MyClassoClass1=newMyClass("sadfsd");//未实现,无法阻止构建。oClass1.doSomething(0.4);//在编译时中断但到目前为止,我还没有没有办法让.net接受参数少于我可以显式调用接口的通用扩展,这非常冗长并且违背了所有这些oClass.doSomething(0.5);的目的;我想用一个包装器来解决这个问题:}publicstaticvoiddoSomething(thisMyClassoTh,Uu){oTh.wrappedDoSomething(u);但是包装器无法解析包装函数的两种类型,失败并显示:错误1??类型“MyClass”不能用作泛型类型或方法“MyClassExtension”。在wrappedDoSomething(P,U)中键入参数“P”。没有从“MyClass”到“ISomePolicy”的隐式引用转换任何关于修复参数问题或重新设计所有这些的见解都值得赞赏。对于上下文,这将用于包装I/O转换器。在我的例子中,T是目标I/O格式,U是我的框架使用的数据的对象表示。我知道这可以通过委托或接口轻松实现,但目标是框架的用户可以轻松实例化他们需要的转换,如果实现不存在,他们可以简单地将其添加到公共接口。编辑:从另一个泛型方法/类中解析一个泛型方法似乎对单声道不起作用。一般来说,策略不应包含数据。例如,interfaceISomePolicy{void_doSomething(Tt,Uu);}structSomePolicyImplementation:ISomePolicy,ISomePolicy,ISomePolicy{voidISomePolicy._doSomething(intt,intu)=>Console.WriteLine("thisisint,int");voidISomePolicy._doSomething(intt,doubleu)=>Console.WriteLine("thisisint,double");voidISomePolicy._doSomething(doublet,doubleu)=>Console.WriteLine("thisisdouble,double");}staticclassSomePolicyExtension{publicstaticvoiddoSomething(thisPpolicy,Tt,Uu)其中P:struct,ISomePolicy=>policy._doSomething(t,u);}如果您想要结合策略和数据,那么您可以考虑不同的界面interfaceIEmbeddedPolicy{void_doSomething(Uu);}类MyClass:IEmbeddedPolicy,IEmbeddedPolicy{publicTValue{get;}publicMyClass(Tvalue){this.Value=value;}voidIEmbeddedPolicy._doSomething(intu)=>Console.WriteLine("thisisT,int");voidIEmbeddedPolicy._doSomething(doubleu)=>Console.WriteLine("这是T,double");}静态类嵌入edPolicyExtension{publicstaticvoiddoSomething(thisEembedded,Uu)whereE:IEmbeddedPolicy=>embedded._doSomething(u);}或这两个概念的组合classMySuperClass:IEmbeddedPolicy,IEmbeddedPolicy其中P:struct,ISomePolicy,ISomePolicy}publicMySuperClass(Tvalue){this.Value=value;}voidIEmbeddedPolicy._doSomething(intu)=>newP()._doSomething(this.Value,u);voidIEmbeddedPolicy._doSomething(doubleu)=>newP()._doSomething(this.Value,u);}用法://独立策略varpolicy=newSomePolicyImplementation();policy.doSomething(5,6);policy.doSomething(5,6.7);policy.doSomething(5.3,6.7);//嵌入式策略varmy=newMyClass(54);我的.doSomething(5);我的.doSomething(89.7);//组合varx=newMySuperClass(53);x.doSomething(9);x.doSomething(18.3);尝试了您的代码,但即使是简单的调用也无法开箱即用主要问题是MyClass包含未知元素类型“myEement”——该类型无法从派生的函数参数中调用。但是-如果您概括并省略对象类型-您的示例将开箱即用:usingSystem;使用System.Collections.Generic;接口ISomePolicy{void_doSomething(Uu);}publicclassMyClass:ISomePolicy,ISomePolicy{内部对象myEement{get;放;}publicMyClass(objectElement){myEement=Element;}voidISomePolicy._doSomething(doubleu){Console.WriteLine("thisisdouble");}voidISomePolicy._doSomething(intu){Console.WriteLine("thisisint");}}staticclassMyClassExtension{publicstaticvoiddoSomething(thisPoTh,Uu)whereP:ISomePolicy{oTh._doSomething(u);}}classProgram{staticvoidMain(){MyClassoClass=newMyClass(3);oClass.doSomething(0.5);//这有效oClass.doSomething(1);//这行得通//oClass.doSomething("Willnotwork");}}什么是myEement(或者您可能指的是myElement)——如有必要,您可以在运行时获取其类型。myElement.GetType(),或转换为它-例如if(myElementisint)DoSomethingWithInt((int)myElement);但是-反思总是会减慢执行速度。如果您不打算创建具有大量实例的超重类层次结构-那么这应该足以满足您的需求。以上就是《C#学习教程:使用策略模式在C#上实现多参数C++模板行为共享》的全部内容。如果对大家有用,需要了解更多C#学习教程,希望大家多加关注——本文来自网络收藏,不代表立场,如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: