简体   繁体   English

转换为类型

[英]Casting to a Type

I'm wondering if it's possible to cast an object to a Type... I've just started using Reflection, so maybe I'm doing it all wrong but here's what I would like to do: 我想知道是否有可能将对象转换为Type ...我刚刚开始使用Reflection,所以也许我做错了所有,但是这是我想做的:

...
Type type = ...;
Type interfaceType = someOtherType.GetInterface("IConverter`2");

return (Cast to interfaceType)Activator.CreateInstance(type);

Is the cast to the interface possible? 是否可以强制转换为接口?

Update: 更新:

Compiler says that T and K can not be found. 编译器说找不到T和K。 The myInterface Type instance knows the T and K class... myInterface Type实例知道T和K类。

public IConverter<T, K> GetConverter(Type type)
{
    if (dtoModelDictionary.ContainsKey(type))
    {
        Type foundType = dtoModelDictionary[type];
        Type myInterface = foundType.GetInterface("IConverter`2");

        return (IConverter<T, K>)Activator.CreateInstance(foundType); 
    }
    else if (dalModelDictionary.ContainsKey(type))
    {
        Type foundType = dalModelDictionary[type];

        return (IConverter<T, K>)Activator.CreateInstance(foundType);
    }
    else
    {
        throw new System.Exception();
    }
}

Second update: 第二次更新:

public SomeClass GetConverter(Type type)
    {
        if (dtoModelDictionary.ContainsKey(type))
        {
            Type foundType = dtoModelDictionary[type];
            Type myInterface = foundType.GetInterface("IConverter`2");

            IConverter<T, K> converter = (IConverter<T, K>)Activator.CreateInstance(foundType); 
            return converter.someMethod(); 
        }
    }

Answer to you update: 回答您更新:

You cannot cast to a type where the generic arguments are not defined. 您不能转换为未定义通用参数的类型。 T and K must be defined for the method that is using it. 必须为使用它的方法定义T和K。

Either declare it: 要么声明它:

public IConverter<T, K> GetConverter<T, K>(Type type)

Or, if you face the problem often that this interface is used but you don't know any T or K types, use an interface without generics: 或者,如果您经常遇到使用此接口但不知道任何T或K类型的问题,请使用没有泛型的接口:

interface IConverter
{ 
  // general members
}

interface  IConverter<T, K> : IConverter 
{
  // typesave members
}

public IConverter GetConverter(Type type)
{
  // ...
  return (IConverter)Activator.CreateInstance(type);
}

Not really, no... at least not in this way. 不是,不是,至少不是这样。 The problem is that your return value is going to have to be whatever your method's return value is typed as. 问题在于您的返回值将必须是方法的返回值键入为的值。 Because everything must be typed at compile-time, there is limited or no real use case that I can see for this particular kind of type coersion - maybe you can say some more about what you are trying to accomplish? 因为必须在编译时键入所有内容,所以对于这种特定类型的类型强制,我只能看到有限的实例或没有实际的用例-也许您可以说说更多有关您要实现的目标?

Now if you are using generics, you do have a run-time typing story, you can return your Type parameter type: 现在,如果您使用泛型,那么您确实有一个运行时键入的故事,您可以返回Type参数类型:

public T MyMethod<T>(...)
...
return (T)Activator.CreateInstance(type);

You can only cast an object to something that it actually is. 您只能将对象投射到实际存在的对象上。 You can for example cast a String reference to IEnumerable , but you can't cast it to char[] . 例如,您可以将String引用IEnumerableIEnumerable ,但不能将其char[]char[]

If what your method returns actually implements the interface, you can just cast it as usual. 如果您的方法返回的结果实际上实现了该接口,则可以照常进行转换。

Example: 例:

return (IConverter<int,string>)Activator.CreateInstance(type);

Edit: 编辑:
You need to make the method generic so that you can specify the data types when you call it: 您需要使该方法通用,以便在调用它时可以指定数据类型:

public IConverter<T, K> GetConverter<T, K>(Type type) {
   ...
}

你可以这样做:

var type = typeof(IConverter<,>).MakeGenericType(new Type[] { typeof(T), typeof(K) });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM