C# 泛型和方法

发布于 2024-07-09 06:15:55 字数 1279 浏览 6 评论 0原文

我怎样才能选择好的方法(我在下面的示例中展示了两种不起作用的不同方法)。 我使用带有 IF 和 IS 的对象类型变量来代替对象类型的变量来完成这项工作,但我试图避免使用对象和装箱/拆箱。 所以我认为 Generic 可以完成这项工作,但我被困在这里。

这是一小段代码,说明了我的问题:

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
{
}

更新

我不需要接口和类。 对不起。 我知道这不是问题的目的。

使用 (ObjectType)obj 进行转换不起作用,但如果你这样做:

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

它起作用......为什么?

而且...我无法为所有类型重载执行方法,因为该方法来自接口。 这就是为什么所有的方法都需要从这个方法中调用。

How can I select the good method (I have in the example below show 2 differents way that doesn't work). 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. So I thought that Generic could do the job but I am stuck here.

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 :

        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. This is why all need to be called from this method.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

蒲公英的约定 2024-07-16 06:15:55

不,你不能这样做。 泛型不像 C++ 模板那样工作——泛型方法只编译一次。 编译器可以用于重载决策的唯一信息是它在泛型方法中了解的信息,无论什么代码使用它。

作为展示这一点的示例,这里有一些代码可能无法按您期望的方式工作:

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
    }
}

如果不了解更多关于您想要做什么的信息,很难说出继续进行的最佳方法。

No, you can't do this. Generics don't work like C++ templates - the generic method is compiled just once. 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.

坏尐絯 2024-07-16 06:15:55

你考虑过接口吗?

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;
   }
}

由OP编辑:

此解决方案需要更改所有业务逻辑对象以具有此接口。 这确实不是一件可以做的事情(在我的情况下)。 而且,在其他情况下,我总是更喜欢拥有干净的 BusinessObject,它没有与业务无关的接口。 在我的问题中,我想要一个与通用/对象/委托方法更相关的解决方案来实现它。 谢谢你。 这个答案不会被接受。

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:

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. In my question, I want a solution that is more related with Generic/Object/Delegate method to achieve it. Thx you. This answer won't be accepted.

策马西风 2024-07-16 06:15:55

类 Parser 有很多私有方法,根据对象类型由执行方法调用。 它需要重定向到好的方法。

编译器将为您完成这项工作。 只需使用重载即可。

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 { }

然后,调用:

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

没有装箱/拆箱,并且调用适当的方法。

The class Parser has a lot of private method that are called by the execute method depending of the object type. 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.

浅紫色的梦幻 2024-07-16 06:15:55

我没试过,但是你能做到吗?

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

(根据评论,不起作用)

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

(根据评论,起作用)

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)

鼻尖触碰 2024-07-16 06:15:55

我知道您担心装箱/拆箱,因此这里可能涉及 ValueTypes。

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

假设该操作正在修改 obj,并且还假设修改对调用者很重要(这就是为什么您将值返回给调用者)。 这段代码有一个令人讨厌的按值传递缺陷。

考虑这段代码:

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

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

以这种方式调用。

int x = p.execute(1);

x 是 1,而不是 2。

I know you're concerned about boxing/unboxing, so there could be ValueTypes involved here.

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). 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.

ら栖息 2024-07-16 06:15:55

泛型发生在编译时。 当您希望相同的代码应用于不同的类型时,最好使用它。 它不是动态的,因此它不会帮助您根据输入类型在方法之间切换。

David B 的回复中的重载解析有效,但也会在编译时发生。

您更新中的代码执行相同的操作。 它进行强制转换(在仔细检查类型之后),然后使用重载来解析该方法。

我觉得你想根据运行时输入切换方法。

如果使用反射,您可以获得更动态的行为。

        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; 
        } 

它可能有点脆弱,但在示例中它可以工作。

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.

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.

月亮坠入山谷 2024-07-16 06:15:55

IIRC,你可以使用“where”子句来允许这

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

一点,我总是必须自己谷歌那个,所以我就留在那里。

编辑:阅读一些评论。 我不建议调用特定于类型的代码。 而是将该代码放入虚拟函数中并调用它。 调用签名可能会很长,但这就是自动完成的目的。

Koodos to joshua.ewer 查找手册页

IIRC you can use the "where" clause to allow this

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

I always have to Google that one my self so I'll leave it at that.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文