Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.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#-将泛型接口强制转换为传递类型的泛型类<;T>;这是在运行时确定的_C#_Generics - Fatal编程技术网

C#-将泛型接口强制转换为传递类型的泛型类<;T>;这是在运行时确定的

C#-将泛型接口强制转换为传递类型的泛型类<;T>;这是在运行时确定的,c#,generics,C#,Generics,如何通过传递在运行时确定的类型来强制转换具有泛型接口的泛型类 public class SomeDataChanged { public string EventName { get; set; } } public class MessageSub<T> where T : class { public IMessageBus Bus { get; set; } public ISubscription<T> Sub {

如何通过传递在运行时确定的类型来强制转换具有泛型接口的泛型类

public class SomeDataChanged
    {
        public string EventName { get; set; }
    }

public class MessageSub<T> where T : class
{
    public IMessageBus Bus { get; set; }
    public ISubscription<T> Sub { get; set; }
}

public interface IDataChangedService<T> where T : class
{
    MessageSub<T> ProcessData(string topic, string subscription);
}

public class DataChangedService<T> : IDisposable, IDataChangedService<T> where T : class
{
    public MessageSub<T> ProcessData(string topic, string subscription)
    {
        // Some code
    }

    // More code
}
公共类SomeDataChanged
{
公共字符串EventName{get;set;}
}
公共类MessageSub,其中T:class
{
公共IMessageBus{get;set;}
公共ISubscription子{get;set;}
}
公共接口IDataChangedService,其中T:class
{
MessageSub ProcessData(字符串主题、字符串订阅);
}
公共类DataChangedService:IDisposable,IDataChangedService其中T:class
{
public MessageSub ProcessData(字符串主题、字符串订阅)
{
//一些代码
}
//更多代码
}
我使用反射将在运行时确定的类型传递给泛型类,使用以下命令。但不知道如何将其转换为另一个泛型类型

class Test
{
    static void Main()
    {
        string topic = "OrderChanged";
        string subscription = "MyUi";
        string typeName = "OrderDataChanged";
        Type T = Type.GetType(typeName);

        Type genericClass = typeof(DataChangedService<>);
        Type constructedClass = genericClass.MakeGenericType(T);

        //How to cast another generic interface to my constructed class?
        var obj = (IDataChangedService<T>)Activator.CreateInstance(constructedClass); 

        //Also how to return generic type object?
        MessageSub<T> msgSub = obj.ProcessData(topic, subscription);

        // and, then some code that use msgSub
    }
}
类测试
{
静态void Main()
{
字符串topic=“OrderChanged”;
string subscription=“MyUi”;
字符串typeName=“OrderDataChanged”;
Type T=Type.GetType(typeName);
类型genericClass=typeof(DataChangedService);
类型constructedClass=genericClass.MakeGenericType(T);
//如何将另一个泛型接口强制转换到我构造的类?
var obj=(IDataChangedService)Activator.CreateInstance(constructedClass);
//另外,如何返回泛型类型对象?
MessageSub msgSub=obj.ProcessData(主题,订阅);
//然后是一些使用msgSub的代码
}
}

您不能执行
IDataChangedService
T
System.type
类型的运行时变量,而不是由字符串
typeName
表示的编译时类型。要使用泛型参数,类型必须是编译时

您需要引入一些非泛型类型来实现这一点

大概是这样的:

public interface IMessageSub { }

public class MessageSub<T> : IMessageSub where T : class
{
    public IMessageBus Bus { get; set; }
    public ISubscription<T> Sub { get; set; }
}

public interface IDataChangedService
{
    IMessageSub ProcessData(string topic, string subscription);
}

public interface IDataChangedService<T> : IDataChangedService where T : class
{
    MessageSub<T> ProcessData(string topic, string subscription);
}

public class DataChangedService<T> : IDataChangedService<T> where T : class
{
    IMessageSub IDataChangedService.ProcessData(string topic, string subscription)
    {
        return this.ProcessData(topic, subscription);
    }

    public MessageSub<T> ProcessData(string topic, string subscription)
    {
        // Some code
    }

    // More code
}

这不能在编译时完成,因为
T
是在运行时定义的。你必须回顾你的设计。
var obj = (IDataChangedService)Activator.CreateInstance(constructedClass);

obj.ProcessData(topic, subscription);