C# 根据参数/常规自定义/覆盖功能
我想通过向类中添加新函数,根据函数参数/泛型类型自定义外部类(自写)的函数 例如,创建一个与此处相同的自定义接口并将其添加到对象中 所以我想为T型做一个自己的交互(例如class2) 将这个接口添加到class1,当我从class1调用函数时,应该使用这个接口/函数C# 根据参数/常规自定义/覆盖功能,c#,.net,C#,.net,我想通过向类中添加新函数,根据函数参数/泛型类型自定义外部类(自写)的函数 例如,创建一个与此处相同的自定义接口并将其添加到对象中 所以我想为T型做一个自己的交互(例如class2) 将这个接口添加到class1,当我从class1调用函数时,应该使用这个接口/函数 object1(of class1).add(Interface x, typeof(class2)) object1(of class1).DoSomething<class2>() -> calls inter
object1(of class1).add(Interface x, typeof(class2))
object1(of class1).DoSomething<class2>() -> calls interface/function x
object1(属于类1)。添加(接口x,类型of(类2))
object1(属于class1).DoSomething()->调用接口/函数x
与此处相同的功能:(但函数DoSomethingY不是在创建实例后添加的,而是需要在class1本身中—我不想这样做)
公共类1{
私人物品。。
私人物品2。。
私有DoSomethingDefault。。
公共卫生服务
{
如果(类型(t)==…)
返回DoSomething1();
其他(类型(T)==…)
返回DoSomething2();
其他的
返回DoSometingDefault();
}
}
所以我的问题是。我应该使用什么:间隔、代理?我在哪里/如何储存这些
编辑:
更清楚一点
public interface IDoSomething<T>
{
public T Work(string obj)
}
public DoSomething1 : IDoSomething<class2>
{
public class2 Work()
{
return new class2() {name = "xx"} {
}
}
var obj1 = new class1();
obj1.Register<class2>(DoSomething1) // might be wrong syntax, but my intention
// or obj1.Register(tyopeof(class2),DoSomething1);
class2 obj2 = obj1.DoSomething<class2>();
//obj1/class1 should call DoSomething1.Work();
class3 obj3 = obj1.DoSomething<class3>(); // should call default if not registered
公共接口
{
公共T形工作(字符串obj)
}
公共剂量1:我有什么
{
公共二级工作()
{
返回新的class2(){name=“xx”}{
}
}
var obj1=新类别1();
obj1.Register(DoSomething1)//可能是错误的语法,但我的意图是
//或obj1.寄存器(TYOPOF(类别2),DoSomething1);
class2 obj2=obj1.DoSomething();
//obj1/class1应该调用DoSomething1.Work();
class3 obj3=obj1.DoSomething();//如果未注册,则应调用默认值
这是一种正常的设计模式吗?调用自定义函数而不是使用默认类函数?如果我没弄错的话。您需要这样的功能:
var _do = new Do();
_do.Register<I1>(() => Console.WriteLine("I1 action"));
_do.Register<I2>(() => Console.WriteLine("I2 action"));
_do.Work<I1>();
_do.Work<I2>();
_do.Work<string>();
public class Do {
IDictionary<Type, Action> actions = new Dictionary<Type, Action>();
private void DefaultAction(){
Console.WriteLine("Default action called");
}
public void Register<T>(Action act){
var type = typeof(T);
if(!actions.ContainsKey(type)){
actions.Add(type, act);
}
}
public void Work<T>()
{
var type = typeof(T);
if(actions.ContainsKey(type))
{
actions[type]();
}
else
{
DefaultAction();
}
}
}
public interface I1 { }
public interface I2 { }
var\u do=new do();
_do.Register(()=>Console.WriteLine(“I1操作”);
_do.Register(()=>Console.WriteLine(“I2操作”);
_做工作;
_做工作;
_做工作;
公共课做什么{
IDictionary actions=新字典();
私有void DefaultAction(){
WriteLine(“调用的默认操作”);
}
公共无效登记册(行动法){
var类型=类型(T);
如果(!actions.ContainsKey(类型)){
动作。添加(类型、动作);
}
}
公共工程()
{
var类型=类型(T);
if(actions.ContainsKey(类型))
{
动作[类型]();
}
其他的
{
DefaultAction();
}
}
}
公共接口I1{}
公共接口I2{}
谢谢!这正是我想要的,但我还不够具体(请参见编辑)。我不希望DoSomething调用接口的函数。我认为这是一种标准的设计模式。不是吗?在工作函数中,使用if(TryGetValue(type,out temp)){temp();}
而不是ContainsKey(type)
,actions[type]
pair,使用TryGetValue,您只需要在字典中执行一次查找,而不需要执行两次。
var _do = new Do();
_do.Register<I1>(() => Console.WriteLine("I1 action"));
_do.Register<I2>(() => Console.WriteLine("I2 action"));
_do.Work<I1>();
_do.Work<I2>();
_do.Work<string>();
public class Do {
IDictionary<Type, Action> actions = new Dictionary<Type, Action>();
private void DefaultAction(){
Console.WriteLine("Default action called");
}
public void Register<T>(Action act){
var type = typeof(T);
if(!actions.ContainsKey(type)){
actions.Add(type, act);
}
}
public void Work<T>()
{
var type = typeof(T);
if(actions.ContainsKey(type))
{
actions[type]();
}
else
{
DefaultAction();
}
}
}
public interface I1 { }
public interface I2 { }