简体   繁体   English

C#反射调用-返回通用对象{类型}-需要类型

[英]C# Reflection Invoke - Returns Generic Object { Type } - Need Type

I have a stored procedure call interface that I'm using to handle results from stored procedures with entity (using Translate method to translate the results of our stored procedure into entities that can be tracked and used in EF as normal) 我有一个存储过程调用接口,用于处理带有实体的存储过程的结果(使用Translate方法将存储过程的结果转换为可以在EF中正常跟踪和使用的实体)

Here's the basic code... 这是基本代码...

List<object> current = new List<object>();
object item = ((Type)currenttype.Current).GetConstructor(System.Type.EmptyTypes).Invoke(new object[0]);

ObjectContext actualContext = ((IObjectContextAdapter)context).ObjectContext;
string className = "";
EntityContainer container = null;
string setName = "";

className = ((Type)currenttype.Current).ToString();
container = actualContext.MetadataWorkspace.GetEntityContainer(((IObjectContextAdapter)context).ObjectContext.DefaultContainerName, DataSpace.CSpace);
setName = (from meta in container.BaseEntitySets
           where meta.ElementType.FullName == className
           select meta.Name).FirstOrDefault();


var t = typeof(ObjectContext).GetMethod("Translate", new Type[] { typeof(DbDataReader), typeof(string), typeof(MergeOption) }).MakeGenericMethod(item.GetType()).Invoke(actualContext, new object[] { reader, setName, MergeOption.AppendOnly });

The issue is that I can't do anything with 't' that I want, it's type is listed as 问题是我无法对想要的“ t”做任何事情,它的类型列为
object {System.Data.Entity.Core.Objects.ObjectResult<POCOClass>} . object {System.Data.Entity.Core.Objects.ObjectResult<POCOClass>} I can't call any of the normal methods that I can normally on the ObjectResult type such as ToArray or ToList . 我无法调用通常可以在ObjectResult类型上使用的任何常规方法,例如ToArrayToList

I need a way to convert it into System.Data.Entity.Core.Objects.ObjectResult<POCOClass> . 我需要一种将其转换为System.Data.Entity.Core.Objects.ObjectResult<POCOClass> The difference being that 't' is listed as type object first. 区别在于't'首先被列为类型对象。

I cannot use any strongly typed casts because the types will change depending on the stored procedure. 我不能使用任何强类型的强制类型转换,因为类型将根据存储过程而改变。 I've tried using the dynamic keyword instead of var for t and I've also tried using Convert.ChangeType . 我尝试使用dynamic关键字代替t的var,也尝试使用Convert.ChangeType It never changes from the object base type. 它永远不会从对象基本类型更改。 dynamic t returns this the following error: dynamic t返回以下错误:

'System.Data.Entity.Core.Objects.ObjectResult<POCOClass>' does not contain a definition for 'ToList' 'System.Data.Entity.Core.Objects.ObjectResult <POCOClass>'不包含'ToList'的定义

Thought I know for a fact it does... 以为我知道事实确实如此...

To clear up confusion, here's a watch screenshot. 为了消除混乱,请看下面的屏幕截图。 The first line is what's being returned, I want it to be like the second (see Type column). 第一行是返回的内容,我希望它像第二行一样(请参阅“类型”列)。

Edit : might be getting closer... I added this: 编辑 :可能越来越近...我添加了这个:

var listedT = typeof(Enumerable).GetMethod("ToList").MakeGenericMethod(item.GetType()).Invoke(null, new object[] { t });
current.AddRange(listedT); // Error here...

listedT becomes a object {System.Collections.Generic.List<ReportCatalog.Models.Catalog_Reports>} and I get the error, cannot convert from object to System.Collections.Generic.IEnumerable<object>. listingT成为object {System.Collections.Generic.List<ReportCatalog.Models.Catalog_Reports>} ,但出现错误,无法从对象转换为System.Collections.Generic.IEnumerable <object>。

在此处输入图片说明

Since ObjectResult<T> implements also the non-generic IEnumerable interface, cast it to this type and enumerate it. 由于ObjectResult<T>还实现了非通用IEnumerable接口,因此将其IEnumerable转换为该类型并枚举它。

var e = (IEnumerable)t;
foreach (object o in e) {
    //TODO: use o
}

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

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