简体   繁体   English

我有 3 个不同的类,具有相同的方法名称。 需要在使用前检查并应用它们的正确实例c#

[英]I have 3 different classes, with the same method names. Need to check before usage and apply the right instance of them c#

I have 3 different classes but the same methods inside them.我有 3 个不同的类,但它们内部的方法相同。 Need to check before usage and apply the right instance of them c#.需要在使用前检查并应用它们的正确实例 c#。 The calling methods are the same but different logic inside.调用方法相同,但内部逻辑不同。 I need to be able to call the right instance at the right time.我需要能够在正确的时间调用正确的实例。 I tried everything and it appears that I am failing.我尝试了一切,但似乎失败了。

Interface is not an option because these assemblies come in the form of DLLs.接口不是一个选项,因为这些程序集以 DLL 的形式出现。 I dont have the source code for them.我没有他们的源代码。

public VersionAssembly240 obj1 = new VersionAssembly240();
public VersionAssembly250 obj2 = new VersionAssembly250();
public VersionAssembly260 obj3 = new VersionAssembly260();

void Main()
{
    ChooseRightInstance(null, obj2, null, "v25");

}

public void ChooseRightInstance(VersionAssembly240 obj1, VersionAssembly250 obj2, VersionAssembly260 obj3, string version)
{
    if (version == "v24")
    {
        var obj = obj1;
    }
    else if (version == "v25")
    {
        var obj = obj2;
    }
    else
    {
        var obj = obj3;
    }

    //I need to be able to access the obj var somce the 3 classess have the same access methods
    obj
}

//for testing
public class VersionAssembly260
{
    public string example()
    {
        return "v26";
    }
}

public class VersionAssembly250
{
    public string example()
    {
        return "v25";
    }
}

public class VersionAssembly240
{
    public string example()
    {
        return "v24";
    }
}

You have to crate an interface with method example() and different implementations for each class.您必须使用方法example()和每个类的不同实现来创建接口。

public interface IVersionAssembly
{
    string example();
}

Implement method example()实现方法example()

public class VersionAssembly260 : IVersionAssembly
{
    public string example()
    {
        return "v26";
    }
}

public class VersionAssembly250 : IVersionAssembly
{
    public string example()
    {
        return "v25";
    }
}

public class VersionAssembly240 : IVersionAssembly
{
    public string example()
    {
        return "v24";
    }
}

Now use interface IVersionAssembly in method ChooseRightInstance现在使用的界面IVersionAssembly的方法ChooseRightInstance

public void ChooseRightInstance(VersionAssembly240 obj1, VersionAssembly250 obj2, VersionAssembly260 obj3, string version)
{
    IVersionAssembly obj;
    if (version == "v24")
    {
        obj = obj1;
    }
    else if (version == "v25")
    {
        obj = obj2;
    }
    else
    {
        obj = obj3;
    }

    obj.example();
}

You could potentially achieve this using an Interface, assuming that the real-world versions of these classes all have the same public methods.您可以使用接口来实现这一点,假设这些类的真实版本都具有相同的公共方法。

Live demo: https://dotnetfiddle.net/63hjQt现场演示: https : //dotnetfiddle.net/63hjQt

Code.代码。 See the comments inline for more info on each important part:有关每个重要部分的更多信息,请参阅内联注释:

using System;

public class Program
{
    public static void Main() {
      VersionAssembly240 obj1 = new VersionAssembly240();
      VersionAssembly250 obj2 = new VersionAssembly250();
      VersionAssembly260 obj3 = new VersionAssembly260();
      ChooseRightInstance( null, obj2,  null ,  "v25" );
    }

    public static void ChooseRightInstance(VersionAssembly240 obj1, VersionAssembly250 obj2, VersionAssembly260 obj3, string version) 
    {
      IVersionAssembly obj; //declare the object outside the if, as an instance of the interface
      if (version == "v24")
      {
        obj = obj1;
      }
      else if (version == "v25")
      {
        obj = obj2;
      }
      else
      {
        obj = obj3;
      }

      Console.WriteLine(obj.example()); //can call the example() method because the interface defines it
    } 
}

//interface which defines the methods which all the classes must implement    
public interface IVersionAssembly
{
    string example();
}

//all the classes implement the interface, but within the methods they can use different concrete implementations
public class VersionAssembly260 : IVersionAssembly
{
    public string example()
    {
        return "v26";
    }
}

public class VersionAssembly250 : IVersionAssembly
{
    public string example()
    {
        return "v25";
    }
}

public class VersionAssembly240 : IVersionAssembly
{
    public string example()
    {
        return "v24";
    }
}

Here's the Microsoft guide to interfaces in C#: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/interfaces/这是微软的 C# 接口指南: https : //docs.microsoft.com/en-us/dotnet/csharp/programming-guide/interfaces/

You can try below你可以试试下面

// make abstract base class
public abstract class BaseVersionAssembly
{
    public abstract string example();
}

public class VersionAssembly260 : BaseVersionAssembly
{
    public override string example()
    {
        return "v26";
    }
}

public class VersionAssembly250 : BaseVersionAssembly
{
    public override string example()
    {
        return "v25";
    }
}

public class VersionAssembly240 : BaseVersionAssembly
{
    public override string example()
    {
        return "v24";
    }


}

ChooseRightInstance function ChooseRightInstance 函数

public string ChooseRightExample(BaseVersionAssembly obj)
{
    return obj.example();
}

暂无
暂无

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

相关问题 C#代码用于处理具有相同方法名称的不同类 - C# code to handle different classes with same method names 使用具有两种相同但名称不同的两种类型的API。 如何重用我的代码? - Consuming an API which has a two types which are the same but have different names. How can I reuse my code? 在C#中使用相同的方法序列化不同的类 - Serialize different classes with same Method in c# C# 存放不同的子类同时调用 - C# store different child classes and call them at same time c# 各种类需要一个扩展方法 - c# need to have one extension method for various classes 我有一个 C# 开关,我需要在每个案例返回之前调用一个方法。 有什么办法可以简化这个吗? - I have a C# switch and I need to call a method before each case returns. Is there any way I could simplify this? 我可以在 C# 的 2 个不同类中使用相同的枚举吗? - Can I use the same enum in 2 different classes in C#? 相同的变量名称 - 2个不同的类 - 如何将值从一个复制到另一个 - 反射 - C# - Same Variable Names - 2 Different Classes - How To Copy Values From One To Another - Reflection - C# C#当从不同的命名空间中使用两组“相同”类时,如何避免两次写入相同的方法? - C# When consuming two sets of “identical” classes from different namespaces, how do I avoid writing the same method(s) twice? C#将不同的文件应用于相同的变量 - C# apply a different file to the same variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM