简体   繁体   English

如何重构重载方法

[英]How to refactor overloaded methods

I have the methods: 我有方法:

public MyReturnType MyMethod(Class1 arg)
{
 //implementation
}

public MyReturnType MyMethod(Class2 arg)
{
 //implementation
}

//...

public MyReturnType MyMethod(ClassN arg)
{
 //implementation
}

decimal, string, DateTime in [Class1, ... , ClassN] 小数,字符串,[Class1,...,ClassN]中的DateTime
and one common method: 和一种常用方法:

public MyReturnType MyMethod(object obj)
{
 if(obj == null)
  throw new ArgumentNullException("obj");
 if(obj is MyClass1)
  return MyMethod((Class1)obj);
 if(obj is MyClass2)
  return MyMethod((Class2)obj);
 //...
 if(obj is MyClassN)
  return MyMethod((ClassN)obj);
 return MyMethod(obj.ToString()); //MyMethod(string) implemented.
}

How can i refactor this code? 我该如何重构这段代码? I can use attributes and component model, something like this: 我可以使用属性和组件模型,如下所示:

public class MyAttribute : Attribute
{
 public Type Type { get; set; }
}

public MyReturnType MyMethod(object obj)
{
    if(obj == null)
        throw new ArgumentNullException("obj");
 var protperty = TypeDescriptor.GetProperties(this, new Attribute[] { new MyAttribute() })
  .Cast<PropertyDescriptor>().FirstOrDefault(x =>
   x.GetAttribute<MyAttribute>().Type.IsInstanceOfType(obj));
 if (protperty != null)
  return protperty.GetValue(obj) as MyReturnType;
 return MyMethod(obj.ToString());
}

but it looks quite hard to understand and can create some bugs. 但它看起来很难理解,可能会产生一些错误。 For example if somebody declare method like 例如,如果某人声明了类似的方法

[MyAttribute(Type = ClassNplus1)]
public NotMyReturnType MyMethod(ClassNplus1 arg);

Any other ideas how to create extensible system, where adding new class is required only to add one method? 任何其他想法如何创建可扩展系统,添加新类只需要添加一个方法? (add code in one place) (在一个地方添加代码)

Sounds like you need to use generic methods: 听起来你需要使用泛型方法:

public MyReturnType MyMethod<T>(T arg)
{
    // implementation
}

The nice thing here is that you can also restrict T like so: 这里的好处是你也可以这样限制T:

public MyReturnType MyMethod<T>(T arg) where T : MyClassBase
{
    // implementation
}

In the second case, you can treat T as if it was an actual MyClassBase , but you're free to pass in any object, as long as it is (or derives from) MyClassBase . 在第二种情况下,您可以将T视为实际的MyClassBase ,但只要它是(或派生自) MyClassBase ,您就可以自由地传入任何对象。 This also works for interfaces as well. 这也适用于接口。

You call this method like so: 你这样称呼这个方法:

MyMethod(new MyClass1());
MyMethod(new MyClass2());
MyMethod(new MyClass3());

The compiler is smart enough to know which type it is so you don't have to pass it the type of T, but sometimes you need to explicitly declare it when calling the method, like so: 编译器足够聪明,可以知道它是哪种类型,因此您不必将其传递给T类型,但有时您需要在调用方法时显式声明它,如下所示:

MyMethod<MyClass1>(new MyClass1());
MyMethod<MyClass2>(new MyClass2());
MyMethod<MyClass3>(new MyClass3());

I believe what you are trying to do is known as multiple dispatch (someone please correct me if I'm wrong) and this is not available in current versions of the .Net framework. 我相信你要做的事情被称为多次发送(有人请纠正我,如果我错了),这在当前版本的.Net框架中是不可用的。 However it is being introduced in .Net 4.0 via the dynamic keyword -> http://blogs.msdn.com/laurionb/archive/2009/08/13/multimethods-in-c-4-0-with-dynamic.aspx . 然而,它是通过动态关键字在.Net 4.0中引入的 - > http://blogs.msdn.com/laurionb/archive/2009/08/13/multimethods-in-c-4-0-with-dynamic.aspx

You could use Generics and Attributes to describe some kind of meta data about the class The Generics will most likely clean up your if statements 你可以使用泛型和属性来描述关于类的某种元数据。泛型很可能会清理你的if语句

Hmmm... 嗯...

public class MyClass<T>
{
   public OtherClass ReturnSomething(T checkThisType)
   {
   }
}

Sorry I could be more descriptive. 对不起,我可能更具描述性。 I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM