说我有这个类 Framework.Asd.Human 和一个公共(public)的空构造函数。我希望能够从字符串输入“Framework.Asd.Human”动态创建它的实例。这是可以实现的吗? (在 Java 和 C# 中)
编辑:
是否也可以传入参数?像 new Framework.Asd.Human("John", 100, 200);(我知道没有类型安全,但在这种情况下没关系)
最佳答案
您可以使用 Activator.CreateInstance 方法的重载:http://msdn.microsoft.com/en-us/library/wcxyzt4d.aspx
像这样使用它:
object myObject = (Framework.Asd.Human)Activator.CreateInstance(TypeOf(Framework.Asd.Human), new object[] { "John", 100, 200 });
对于 Java:https://www.cs.auckland.ac.nz/references/java/java1.5/tutorial/reflect/object/arg.html
关于c# - java C# vb : how do we dynamically create an instance of a class from a string input (class name),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5768588/