简体   繁体   English

C#泛型和方法

[英]C# Generic and method

How can I select the good method (I have in the example below show 2 differents way that doesn't work). 如何选择好的方法(在下面的示例中,我将显示2种无效的方法)。 I was using instead of a variable of type Object with a IF and IS to do the job but I am trying to avoid using Object and boxing/unboxing. 我使用的不是带有IF和IS的Object类型的变量来完成这项工作,但我试图避免使用Object和装箱/拆箱。 So I thought that Generic could do the job but I am stuck here. 所以我以为Generic可以胜任这项工作,但我被困在这里。

Here is a small snippet of code that illustrate my question: 这是一小段代码,阐明了我的问题:

class Program
{
    static void Main(string[] args)
    {
        Parser p = new Parser();
        ObjectType1 o1 = new ObjectType1();
        p.execute(o1);
        Console.Read();
    }
}

class Parser
{
    public T execute<T>(T obj)
    {
        /*
        if (obj is ObjectType1)
            this.action((ObjectType1)obj);
        else if (obj is ObjectType2)
            this.action((ObjectType2)obj);
        */
        this.action(obj);
        return obj;
    }

    private void action(ObjectType1 objectType1)
    {
        Console.WriteLine("1");
    }

    private void action(ObjectType2 objectType2)
    {
        Console.WriteLine("2");
    }
}


class ObjectType1
{
}

class ObjectType2
{
}

Update 更新资料

I do not want interface and class. 我不要界面和类。 Sorry. 抱歉。 I knew that it's not the goal of the question. 我知道这不是问题的目的。

Casting with (ObjectType)obj doesn't work but if you do : 使用(ObjectType)obj进行投射不起作用,但是如果您这样做:

        if (obj is ObjectType1)
            this.action(obj as ObjectType1);
        else if (obj is ObjectType2)
            this.action(obj as ObjectType1);

it works... why? 它有效...为什么?

And... I cannot overload for all type the execute method because this method is from an Interface. 而且...我无法为所有类型的execute方法重载,因为该方法来自Interface。 This is why all need to be called from this method. 这就是为什么需要从此方法调用所有对象的原因。

No, you can't do this. 不,你不能这样做。 Generics don't work like C++ templates - the generic method is compiled just once. 泛型不像C ++模板那样工作-泛型方法仅编译一次。 The only information that the compiler can use for overload resolution is the information it knows about within the generic method, regardless of what code uses it. 编译器可用于重载解析的唯一信息是它在通用方法中了解的信息,而不管使用什么代码。

As an example to show this, here's a bit of code which may not work how you expect it to: 作为显示此示例的示例,以下是一些代码,可能无法按预期运行:

using System;

class Test
{    
    static void Main()
    {
        string x = "hello";
        string y = string.Copy(x);

        Console.WriteLine(x==y); // Overload used
        Compare(x, y);
    }

    static void Compare<T>(T x, T y) where T : class
    {
        Console.WriteLine(x == y); // Reference comparison
    }
}

It's hard to say the best way to proceed without knowing more about what you want to do. 在不进一步了解您想做什么的情况下,很难说出最佳的前进方法。

Have you considered interfaces? 您考虑过接口吗?

interface IAction
{
   void action();
}

class ObjectType1 : IAction
{
   void action() {
      Console.WriteLine("1");
   }
}

class ObjectType2 : IAction
{
    void action() {
      Console.WriteLine("2");
    }
}

class Parser
{
   public IAction execute(IAction obj)
   {
      obj.action();
      return obj;
   }
}

Edited by OP: 由OP编辑:

This solution would require to change all Business Logic Object to have this interface. 此解决方案将需要更改所有业务逻辑对象以具有此接口。 This is really not a thing to do (in my situation). 在我看来,这确实不是要做的事情。 And, in other situation, I always prefer to have clean BusinessObject that doesn't have Interface not related with Business stuff. 而且,在其他情况下,我总是更喜欢干净的BusinessObject,而该Object的Interface与业务无关。 In my question, I want a solution that is more related with Generic/Object/Delegate method to achieve it. 在我的问题中,我想要一个与Generic / Object / Delegate方法更相关的解决方案来实现。 Thx you. 谢谢你 This answer won't be accepted. 此答案将不被接受。

I haven't tried it, but can you do this? 我没有尝试过,但是您可以这样做吗?

 
 
 
  
  public T execute<T>(T obj) { this.action((T)obj); return obj; }
 
  

(according to comments, doesn't work) (根据评论,不起作用)

or 要么

public T execute<T>(T obj)
{
    this.action(obj as T);
    return obj;
}

(according to comments, works) (根据评论,作品)

The class Parser has a lot of private method that are called by the execute method depending of the object type. 解析器类具有许多私有方法,这些私有方法由execute方法根据对象类型调用。 It needs to redirect to the good method. 它需要重定向到好的方法。

The compiler will do this work for you. 编译器将为您完成这项工作。 Just use overloads. 只需使用重载即可。

class Parser
{
    public ObjectType1 action(ObjectType1 objectType1)
    {
        Console.WriteLine("1");
        return objectType1;
    }
    public ObjectType2 action(ObjectType2 objectType2)
    {
        Console.WriteLine("2");
        return objectType2;
    }
}

class ObjectType1 { }
struct ObjectType2 { }

Then, called with: 然后,调用:

Parser p = new Parser();
p.action(new ObjectType1());
p.action(new ObjectType2());

There's no boxing/unboxing, and the appropriate method gets called. 没有装箱/拆箱,并且调用了适当的方法。

I know you're concerned about boxing/unboxing, so there could be ValueTypes involved here. 我知道您担心装箱/拆箱,因此此处可能涉及ValueType。

public T execute<T>(T obj)   
{        
    this.action(obj);
    return obj;
}

Supposing that action is modifying obj, and also supposing that modification is important to the caller (which is why you're returning the value back to the caller). 假设该操作正在修改obj,还假设该修改对调用方很重要 (这就是为什么要将值返回给调用方的原因)。 This code has a nasty pass-by-value defect. 该代码有一个讨厌的传递值缺陷。

Consider this code: 考虑以下代码:

    public int execute(int obj)   
    {        
        this.action(obj);
        return obj;
    }

    public void action(int obj)
    {
        obj = obj + 1;
    }

Called in this way. 以这种方式调用。

int x = p.execute(1);

x is 1, not 2. x是1,不是2。

IIRC you can use the "where" clause to allow this IIRC,您可以使用“ where”子句来允许

public T execute<T>(T obj) where : /* somthing */
{
}

I always have to Google that one my self so I'll leave it at that. 我总是不得不向Google透露自己的看法,因此我将其保留下来。

edit: reading some comments. 编辑:阅读一些评论。 I would not advise calling type specific code. 我不建议调用特定于类型的代码。 Rather put that code in a virtual function and call that. 而是将代码放在虚函数中并调用它。 The call signature might get long, but that's what auto complete is for. 呼叫签名可能会很长,但这就是自动完成的目的。

Koodos to joshua.ewer for finding the man page Koodos到joshua.ewer查找手册页

Generics happens in compile time. 泛型发生在编译时。 It is best used when you want the same code to apply to different types. 当您希望同一代码应用于不同类型时,最好使用此方法。 It is not dynamic, so it won't help you switch between methods depending on input types. 它不是动态的,因此不会帮助您根据输入类型在方法之间进行切换。

Overloading resolving as in David B's reply works, but also happens during compile time. 像David B的回复一样,重载解析也可以,但是在编译时也会发生。

The code in your update does the same thing. 更新中的代码执行相同的操作。 It casts (after careful checking of types) and then uses overloading to resolve the method. 它进行转换(在仔细检查类型之后),然后使用重载来解析该方法。

I feel that you want to switch methods based on runtime input. 我觉得您想根据运行时输入切换方法。

You could get a more dynamic behaviour if you used Reflection. 如果使用反射,则可以得到更动态的行为。

        public object execute(object obj) 
        {
            MethodInfo m = typeof(Parser).GetMethod(
                "action", 
                BindingFlags.Instance | BindingFlags.NonPublic, 
                null, 
                new Type[] { obj.GetType() }, 
                null);
            m.Invoke(this, new object[] { obj });
            return obj; 
        } 

It is perhaps a little fragile, but it works in the example. 它可能有点脆弱,但是在示例中可以使用。

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

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