简体   繁体   English

如何将 Invoke 返回的值转换为 IEnumerable<type> type 是 T 类型的变量吗?

[英]How to cast value returned by Invoke to IEnumerable<type> where type is some variable of type T?

I have the situation, where I want to call some generic method on another object and get IEnumerable result.我有这样的情况,我想在另一个对象上调用一些通用方法并获得IEnumerable结果。

private void SomeFunction(Type type)
{
     var method = context.GetType()
       .GetMethods()
       .FirstOrDefault(_ => _.Name == "GetStorage" && _.IsGenericMethod);

     var storage = getStorage.MakeGenericMethod(type)
                    .Invoke(context, new object[] {})
                    .AsEnumerable();
                    //Some magic needed here. Something like Cast<type>, 
                    //but type - variable
     //More code ...
}

Could anyone suggest me how to figure out this situation.谁能建议我如何弄清楚这种情况。 Thank you.谢谢你。

I have already seen this and similar questions: Casting Results from Generic Method Invocation?我已经看到了这个和类似的问题: Casting Results from Generic Method Invocation? But they doesn't answer on my question, how to do same, when I don't know type, to which I want to cast, and type is stored as variable.但是他们没有回答我的问题,如何做同样的事情,当我不知道类型时,我想转换,并且类型存储为变量。

I can't make SomeFunction a generic method, because the real situation is that I am iterating some list with System.Type and calling lambda (ie SomeFunction ) on each element我不能使SomeFunction成为通用方法,因为实际情况是我正在使用System.Type迭代某个列表并在每个元素上调用 lambda(即SomeFunction

There are some things you need to do to get what you want.有些事情你需要做才能得到你想要的。 You say you want to have a lambda, but that means that you need to define that lambda, which is on a type you do not know yet.你说你想要一个 lambda,但这意味着你需要定义那个 lambda,它是一个你还不知道的类型。 You can redesign your lambda into an interface.您可以将 lambda 重新设计为界面。

Also, I find it much easier to define a generic class that does exactly what I want.此外,我发现定义一个完全符合我要求的泛型类要容易得多。 By creating an instance of this class through reflection, and only there, I can implement the rest of the class in a strong typed way.通过反射创建这个类的一个实例,只有在那里,我才能以强类型的方式实现类的其余部分。 This takes away the 'not knowing what type I have' in most places.这在大多数地方消除了“不知道我有什么类型”。

Like this.像这样。 First, the executor interface:一、执行器界面:

public interface ISomeFunctionExecutor
{
    void Execute(SomeContext context);
}

Then the interface that I need to implement on the entities, which is the lambda so to speak.然后是我需要在实体上实现的接口,可以说是 lambda。

public interface IEntityWithSomeFunction
{
    void SomeFunction();
}

Now the implementation of the executor.现在执行executor。

public class SomeFunctionExecutor<TType> : ISomeFunctionExecutor
{
    public void Execute(SomeContext context)
    {
        var data = context.GetStorage<TType>().Cast<IEntityWithSomeFunction>();
        foreach (var item in data)
        {
            item.SomeFunction();
        }
    }
}

And finally, the usage of it all:最后,这一切的用法:

// Usage:
SomeContext context = new SomeContext();
Type type = typeof(SomeEntity);
var executorType = typeof(SomeFunctionExecutor<>).MakeGenericType(type);
var executor = Activator.CreateInstance(executorType) as ISomeFunctionExecutor;
if (executor != null)
{
    executor.Execute(context);
}

Basically the point is: define a generic class to do what you need to do where you do know the type, and create an instance of this class using reflection.基本上重点是:定义一个泛型类来做你需要做的你知道类型的事情,并使用反射创建这个类的一个实例。 It makes it much easier than having a whole method where you do not know the type.它比拥有一个您不知道类型的完整方法要容易得多。

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

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