C# 如何在给定类名的情况下获取用户定义的对象类型?
如果我将用户定义对象的类名作为字符串,如何在泛型函数中将其用作对象的类型C# 如何在给定类名的情况下获取用户定义的对象类型?,c#,generics,C#,Generics,如果我将用户定义对象的类名作为字符串,如何在泛型函数中将其用作对象的类型 SomeGenericFunction(objectID) 查看System.Type.GetType()方法-提供完全限定的类型名,然后返回相应的类型对象。然后,您可以执行以下操作: namespace GenericBind { class Program { static void Main(string[] args) { Type t = Type.GetType(
SomeGenericFunction(objectID) 查看System.Type.GetType()方法-提供完全限定的类型名,然后返回相应的类型对象。然后,您可以执行以下操作:
namespace GenericBind {
class Program {
static void Main(string[] args) {
Type t = Type.GetType("GenericBind.B");
MethodInfo genericMethod = typeof(Program).GetMethod("Method");
MethodInfo constructedMethod = genericMethod.MakeGenericMethod(t);
Console.WriteLine((string)constructedMethod.Invoke(null, new object[] {new B() }));
Console.ReadKey();
}
public static string Method<T>(T obj) {
return obj.ToString();
}
}
public class B {
public override string ToString() {
return "Generic method called on " + GetType().ToString();
}
}
}
namespace-GenericBind{
班级计划{
静态void Main(字符串[]参数){
Type t=Type.GetType(“GenericBind.B”);
MethodInfo genericMethod=typeof(Program).GetMethod(“方法”);
MethodInfo constructedMethod=genericMethod.MakeGenericMethod(t);
WriteLine((string)constructedMethod.Invoke(null,新对象[]{new B()}));
Console.ReadKey();
}
公共静态字符串方法(T obj){
返回obj.ToString();
}
}
公共B级{
公共重写字符串ToString(){
返回“在“+GetType().ToString()上调用的泛型方法”;
}
}
}
查看System.Type.GetType()方法-提供完全限定的类型名称,然后返回相应的类型对象。然后,您可以执行以下操作:
namespace GenericBind {
class Program {
static void Main(string[] args) {
Type t = Type.GetType("GenericBind.B");
MethodInfo genericMethod = typeof(Program).GetMethod("Method");
MethodInfo constructedMethod = genericMethod.MakeGenericMethod(t);
Console.WriteLine((string)constructedMethod.Invoke(null, new object[] {new B() }));
Console.ReadKey();
}
public static string Method<T>(T obj) {
return obj.ToString();
}
}
public class B {
public override string ToString() {
return "Generic method called on " + GetType().ToString();
}
}
}
namespace-GenericBind{
班级计划{
静态void Main(字符串[]参数){
Type t=Type.GetType(“GenericBind.B”);
MethodInfo genericMethod=typeof(Program).GetMethod(“方法”);
MethodInfo constructedMethod=genericMethod.MakeGenericMethod(t);
WriteLine((string)constructedMethod.Invoke(null,新对象[]{new B()}));
Console.ReadKey();
}
公共静态字符串方法(T obj){
返回obj.ToString();
}
}
公共B级{
公共重写字符串ToString(){
返回“在“+GetType().ToString()上调用的泛型方法”;
}
}
}
如果您有一个字符串,那么首先要做的是使用Type.GetType(string)
,或者(最好)使用Assembly.GetType(string)
来获取Type
实例。在此基础上,您需要使用反射:
Type type = someAssembly.GetType(typeName);
typeof(TypeWithTheMethod).GetMethod("SomeGenericFunction")
.MakeGenericMethod(type).Invoke({target}, new object[] {objectID});
其中,{target}
是实例方法的实例,null
是静态方法的实例
例如:
using System;
namespace SomeNamespace {
class Foo { }
}
static class Program {
static void Main() {
string typeName = "SomeNamespace.Foo";
int id = 123;
Type type = typeof(Program).Assembly.GetType(typeName);
object obj = typeof(Program).GetMethod("SomeGenericFunction")
.MakeGenericMethod(type).Invoke(
null, new object[] { id });
Console.WriteLine(obj);
}
public static T SomeGenericFunction<T>(int id) where T : new() {
Console.WriteLine("Find {0} id = {1}", typeof(T).Name, id);
return new T();
}
}
使用系统;
名称空间名称空间{
类Foo{}
}
静态类程序{
静态void Main(){
string typeName=“SomeNamespace.Foo”;
int id=123;
Type Type=typeof(程序).Assembly.GetType(类型名);
object obj=typeof(Program).GetMethod(“SomeGenericFunction”)
.MakeGenericMethod(类型).Invoke(
null,新对象[]{id});
控制台写入线(obj);
}
公共静态T SomeGenericFunction(int id),其中T:new(){
WriteLine(“Find{0}id={1}”,typeof(T).Name,id);
返回新的T();
}
}
如果您有一个字符串,那么首先要做的是使用Type.GetType(string)
,或者(最好)使用Assembly.GetType(string)
来获取Type
实例。在此基础上,您需要使用反射:
Type type = someAssembly.GetType(typeName);
typeof(TypeWithTheMethod).GetMethod("SomeGenericFunction")
.MakeGenericMethod(type).Invoke({target}, new object[] {objectID});
其中,{target}
是实例方法的实例,null
是静态方法的实例
例如:
using System;
namespace SomeNamespace {
class Foo { }
}
static class Program {
static void Main() {
string typeName = "SomeNamespace.Foo";
int id = 123;
Type type = typeof(Program).Assembly.GetType(typeName);
object obj = typeof(Program).GetMethod("SomeGenericFunction")
.MakeGenericMethod(type).Invoke(
null, new object[] { id });
Console.WriteLine(obj);
}
public static T SomeGenericFunction<T>(int id) where T : new() {
Console.WriteLine("Find {0} id = {1}", typeof(T).Name, id);
return new T();
}
}
使用系统;
名称空间名称空间{
类Foo{}
}
静态类程序{
静态void Main(){
string typeName=“SomeNamespace.Foo”;
int id=123;
Type Type=typeof(程序).Assembly.GetType(类型名);
object obj=typeof(Program).GetMethod(“SomeGenericFunction”)
.MakeGenericMethod(类型).Invoke(
null,新对象[]{id});
控制台写入线(obj);
}
公共静态T SomeGenericFunction(int id),其中T:new(){
WriteLine(“Find{0}id={1}”,typeof(T).Name,id);
返回新的T();
}
}
我想我们需要更多的信息。你能不能写一些你正在尝试做的事情的示例代码,即使它不起作用?我想我们需要更多的信息。您可以编写一些示例代码,说明您正在尝试做的事情,即使它不起作用吗?私有类TestClass:BaseClass{public TestClass(long id):base(id){}}namespace main{public MyClass{private string className;private long classID;//构造函数MyClass(BaseClass obj1){className=obj1.GetType().FullName;classID=obj1.ID;}public void FindObject(){//find from database calling function//需要在给定className的以下函数中传入T.FindByID(classID)}}}}}私有类TestClass:BaseClass{public TestClass(long ID):base(ID){}}namespace main{public MyClass{private string className;private long classID;//构造函数MyClass(BaseClass obj1){className=obj1.GetType().FullName;classID=obj1.ID;}public void FindObject(){//find from database calling function//需要在给定classname的以下函数中传入T.FindByID(classID)}