C# 反射:如何从字符串中获取类引用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1044455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# Reflection: How to get class reference from string?
提问by rutger
I want to do this in C#, but I don't know how:
我想在 C# 中做到这一点,但我不知道如何:
I have a string with a class name -e.g: FooClass
and I want to invoke a (static) method on this class:
我有一个类名的字符串 -eg:FooClass
并且我想在这个类上调用一个(静态)方法:
FooClass.MyMethod();
Obviously, I need to find a reference to the class via reflection, but how?
显然,我需要通过反射找到对类的引用,但是如何呢?
采纳答案by Andrew Hare
You will want to use the Type.GetType
method.
您将想要使用该Type.GetType
方法。
Here is a very simple example:
这是一个非常简单的例子:
using System;
using System.Reflection;
class Program
{
static void Main()
{
Type t = Type.GetType("Foo");
MethodInfo method
= t.GetMethod("Bar", BindingFlags.Static | BindingFlags.Public);
method.Invoke(null, null);
}
}
class Foo
{
public static void Bar()
{
Console.WriteLine("Bar");
}
}
I say simplebecause it is very easy to find a type this way that is internal to the same assembly. Please see Jon's answerfor a more thorough explanation as to what you will need to know about that. Once you have retrieved the type my example shows you how to invoke the method.
我说简单是因为通过这种方式很容易找到同一程序集内部的类型。请参阅Jon 的回答,以更全面地解释您需要了解的内容。检索类型后,我的示例将向您展示如何调用该方法。
回答by GvS
Via Type.GetTypeyou can get the type information. You can use this class to get the methodinformation and then invokethe method (for static methods, leave the first parameter null).
通过Type.GetType可以获得类型信息。您可以使用该类获取方法信息,然后调用该方法(对于静态方法,将第一个参数留空)。
You might also need the Assembly nameto correctly identify the type.
您可能还需要程序集名称来正确识别类型。
If the type is in the currently executing assembly or in Mscorlib.dll, it is sufficient to supply the type name qualified by its namespace.
如果类型在当前执行的程序集中或 Mscorlib.dll 中,则提供由其命名空间限定的类型名称就足够了。
回答by Jon Skeet
You can use Type.GetType(string)
, but you'll need to know the fullclass name including namespace, and if it's not in the current assembly or mscorlib you'll need the assembly name instead. (Ideally, use Assembly.GetType(typeName)
instead - I find that easier in terms of getting the assembly reference right!)
您可以使用Type.GetType(string)
,但您需要知道包括命名空间在内的完整类名,如果它不在当前程序集或 mscorlib 中,您将需要程序集名称。(理想情况下,Assembly.GetType(typeName)
改为使用- 我发现在正确获得程序集参考方面更容易!)
For instance:
例如:
// "I know String is in the same assembly as Int32..."
Type stringType = typeof(int).Assembly.GetType("System.String");
// "It's in the current assembly"
Type myType = Type.GetType("MyNamespace.MyType");
// "It's in System.Windows.Forms.dll..."
Type formType = Type.GetType ("System.Windows.Forms.Form, " +
"System.Windows.Forms, Version=2.0.0.0, Culture=neutral, " +
"PublicKeyToken=b77a5c561934e089");
回答by Atul Chaudhary
Bit late for reply but this should do the trick
回复有点晚,但这应该可以解决问题
Type myType = Type.GetType("AssemblyQualifiedName");
your assembly qualified name should be like this
你的程序集限定名应该是这样的
"Boom.Bam.Class, Boom.Bam, Version=1.0.0.262, Culture=neutral, PublicKeyToken=e16dba1a3c4385bd"
回答by André Voltolini
A simple use:
一个简单的使用:
Type typeYouWant = Type.GetType("NamespaceOfType.TypeName, AssemblyName");
Sample:
样本:
Type dogClass = Type.GetType("Animals.Dog, Animals");
回答by Amol Bhor
We can use
我们可以用
Type.GetType()
Type.GetType()
to get class name and can also create object of it using Activator.CreateInstance(type);
获取类名,也可以使用创建它的对象 Activator.CreateInstance(type);
using System;
using System.Reflection;
namespace MyApplication
{
class Application
{
static void Main()
{
Type type = Type.GetType("MyApplication.Action");
if (type == null)
{
throw new Exception("Type not found.");
}
var instance = Activator.CreateInstance(type);
//or
var newClass = System.Reflection.Assembly.GetAssembly(type).CreateInstance("MyApplication.Action");
}
}
public class Action
{
public string key { get; set; }
public string Value { get; set; }
}
}