Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 字符串值的泛型类型_C#_String_Parsing_Generics_Types - Fatal编程技术网

C# 字符串值的泛型类型

C# 字符串值的泛型类型,c#,string,parsing,generics,types,C#,String,Parsing,Generics,Types,我有一个自定义类,它依赖于要传递的泛型类型T。 我只知道它是什么类型的字符串形式,因为这就是它被发送的方式。我一直在四处寻找,但似乎找不到我真正需要的东西。 我可以将字符串值解析为类型,但我需要将其解析为。。。我可以作为泛型参数传递的东西 我重写了我的问题,如下所示: // Classes structure namespace Mynamespace { public interface IRequest { } public interface IHand

我有一个自定义类,它依赖于要传递的泛型类型T。 我只知道它是什么类型的字符串形式,因为这就是它被发送的方式。我一直在四处寻找,但似乎找不到我真正需要的东西。 我可以将字符串值解析为类型,但我需要将其解析为。。。我可以作为泛型参数传递的东西


我重写了我的问题,如下所示:

// Classes structure
namespace Mynamespace
{
    public interface IRequest
    {
    }

    public interface IHandler<T> where T : IRequest
    {
        void Handle(T item);
    }

    public class MyRequest : IRequest
    {
    }

    public class MyHandler : IHandler<MyRequest>
    {
        void Handle(MyRequest item)
        {
        }
    }
}

// The info I get, and I know typeString is a IRequest
string typeString = "My";
object requestItem = [insert xml parsing here];

// I then create a handler, to handle the request
Type typeHandler = Type.GetType("Mynamespace." + typeString + "Handler");
var handler = Activator.CreateInstance(typeHandler);

Type typeRequest = Type.GetType("Mynamespace." + typeString + "Request");

// what I want to do:
handler.Handle(requestItem);

您需要
类型。MakeGenericType

Type typeArgument = Type.GetType(string.Format("Mynamespace.{0}", typeString));
Type template = typeof(MyClass<>);

Type genericType = template.MakeGenericType(typeArgument);

object instance = Activator.CreateInstance(genericType);
typeArgument=Type.GetType(string.Format(“Mynamespace.{0}”,typeString));
类型模板=类型(MyClass);
类型genericType=template.MakeGenericType(类型参数);
对象实例=Activator.CreateInstance(genericType);

请注意,您不能将其强制转换为特定的
MyClass
,因为您不知道
t
——但它将在执行时成为正确类的实例。

我找到了它,我使用InvokeMember访问了它。:)

Type closedType = typeof(MyClass<>).MakeGenericType(myGeneric);
object obj = Activator.CreateInstance(closedType);

+1.还要注意的是,虽然您不能将结果强制转换为特定的
MyClass
,但您可能需要定义一个非泛型接口(例如
IMyClass
)并强制转换为该接口。@Daniel:的确如此。我想在OP提供更多信息之前,我不会这么做:)这是我已经做过的,创建一个新实例。但这会返回一个对象,而这个对象,我需要解析为一个MyClass。因为“MyClass”有一个空白,需要T。@Daniel:在我的实际代码中,我使用了一个接口,中间还有一个额外的类,但我在这里尝试了一个示例,说明我的问题是什么,而不是让它太复杂。:)@弗里克:什么意思,“有一个需要T的空洞”?您将无法基于仅在运行时确定的类型使用编译时泛型分派——这根本不可能。也许
dynamic
会满足您的需求,但我对您的问题还不太了解,无法确定。
Type closedType = typeof(MyClass<>).MakeGenericType(myGeneric);
object obj = Activator.CreateInstance(closedType);
var obj = (ISomeInterface)Activator.CreateInstance(closedType);
obj.SomeMethodOnTheNonGenericInterface();
typeHandler.InvokeMember("Handle", BindingFlags.InvokeMethod, null, handler, new[] { requestItem });