您可以使用以下解决方案来解决您的问题,这对您的实施有帮助。
--> 首先你创建一个通用接口,它可以用于所有相同类型的通用标识符
--> 按版本名称创建单独的命名空间
DefaultNameSpace : ABC.XYZ
Version : 1.6.2
Then make the namespace patterns as
e.g. ABC.XYZ.V162 (Replcing . and set prefix as per classname norms (always start with Alphabet ) )
Create Class under above namespace with implementing interface
--> 为所有版本创建相同的类名(例如,在 v1 和 v2 版本中通用的 class1、class2 具有不同的实现)
--> 在下面创建常用函数以生成相关对象
public static iTestInterface GetEntity(string className)
{
string versionPrefix = "v_";
string strVersion = 1.6.2;
string dllPath =System.Web.HttpRuntime.BinDirectory;
string dllName = "dllName.dll";
string Version = versionPrefix +
string strclassNameWithFullPath = dllPath + Version.Replace(".", "") + "." + className;
try
{
string strAssemblyWithPath = string.Concat(dllPath, dllName);
if (!System.IO.File.Exists(strAssemblyWithPath))
{
return null;
}
System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFile(strAssemblyWithPath);
Type t = assembly.GetType(strclassNameWithFullPath);
object obj = Activator.CreateInstance(t);
return (iTestInterface)obj;
}
catch (Exception exc)
{
//string errStr = string.Format("Error occured while late assembly binding. dllPath = {0}, dllName = {1}, className = {2}.", dllPath, dllName, className);
return null;
}
}
--> 调用函数如下
iTestInterface obj = GetEntity(classnameString);
--> 调用相关的对象方法。以上调用对于所有相关类都是通用的。
感谢和问候 Shailesh Chopra