C#从字符串名称实例化一个类 [英] C# Instantiate a Class from String name
本文介绍了C#从字符串名称实例化一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public class Method100Response201
{
public string R1_01 { get; set; }
public void DoSpecialThing()
{ Console.WriteLine ("Something Blue..}"); }
}
public class Method100Response404
{
public string R2_01 { get; set; }
public void DoSpecialThing()
{ Console.WriteLine ("Something Old..}"); }
}
public class Method110Response200
{
public string R3_01 { get; set; }
public void DoSpecialThing()
{ Console.WriteLine ("Something New..}"); }
}
它们都在同一个命名空间中的Program类中,我有机制来找出我需要哪个类:
static void Main(string[] args)
{
int[] MethodResponse = DoSomethingHere (23, "something", true);
string DerivedClassName = ResponseModel(MethodResponse[0], MethodResponse[1]);
Console.WriteLine (
"For method " + MethodResponse[0].ToString ()
+ " and response " + MethodResponse[1].ToString ()
+ " you must instantiate Class "
+ DerivedClassName);
Console.ReadKey ();
//how do I do this????
//const string objectToInstantiate = "MyProject.Domain.MyNewTestClass, MyTestProject";
//var objectType = Type.GetType (objectToInstantiate);
//dynamic instantiatedObject = Activator.CreateInstance (objectType) as ITestClass;
// set a property value
//instantiatedObject.Name = DerivedClassName;
// get a property value
//string name = instantiatedObject.Name;
// call a method - output "Something Blue.."
//Console.Write (instantiatedObject.DoSpecialThing ());
}
public static int[] DoSomethingHere (int param1, string param2, bool whatever)
{
int firstInt = 0; int secondInt = 0;
//
//lots of work here, I end up with method and selector..
//
firstInt = 100;
secondInt = 201;
return new int[] { firstInt, secondInt };
}
public static string ResponseModel(int method, int response)
{
string returnClass = String.Empty;
switch (method)
{
case 100:
if (response == 201)
{ Console.WriteLine ("Case 100,201"); returnClass = "Method100Response201"; }
else
{ Console.WriteLine ("Case 100,404"); returnClass = "Method100Response404"; }
break;
case 110:
Console.WriteLine ("Case 100,404"); returnClass = "Method110Response200";
break;
case 120:
// ...
break;
}
return returnClass;
}
我试过一种叫做激活器的东西,我不是专家,这对我来说真的很关键。有没有人能帮帮我?(我留下了对我正在尝试的一些代码的评论,基于在So中发布的解决方案。谢谢)。
推荐答案
这个技术叫反射,意思是从字符串调用实例。 我的调用类将是
public class Class1
{
public string Property { get; set; } = "I'm class1";
public void DoSpecialThings()
{
Console.WriteLine("Class1 does special things");
}
}
接下来,我在一个静态函数中创建一个实例,应该将您的所有类放在同一个命名空间中以便于控制
public static dynamic GetClassFromString(string className)
{
var classAddress = $"NetCoreScripts.{className}";
Type type = GetType(classAddress);
// Check whether the class is existed?
if (type == null)
return null;
// Then create an instance
object instance = Activator.CreateInstance(type);
return instance;
}
和GetType方法
public static Type GetType(string strFullyQualifiedName)
{
Type type = Type.GetType(strFullyQualifiedName);
if (type != null)
return type;
foreach (var asm in AppDomain.CurrentDomain.GetAssemblies())
{
type = asm.GetType(strFullyQualifiedName);
if (type != null)
return type;
}
return null;
}
我用动态类型来快速实现,基本上可以用接口显式编码。
static async Task Main(string[] args)
{
dynamic instance = GetClassFromString("Class1");
Console.WriteLine(instance.GetType().FullName); //NetCoreScripts.Class1
Console.WriteLine(instance.GetType().Name); //Class1
Console.WriteLine(instance.Property); //I'm class1
instance.Property = "Class1 has been changed";
Console.WriteLine(instance.Property); //Class1 has been changed
instance.DoSpecialThings(); // Class1 does special things
}
这篇关于C#从字符串名称实例化一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文