[英]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.