vendredi 13 janvier 2017

C# Reflection Invoke - Returns Generic Object

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)

Here's the basic code...

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 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. I need a way to convert it into System.Data.Entity.Core.Objects.ObjectResult<POCOClass>. The difference being that 't' is listed as type object first.

I cannot use any strongly typed casts because the types will change depending on the SPROC.





Aucun commentaire:

Enregistrer un commentaire