c# - Instantiating DbEntityEntry<T> with Reflection -
i'm attempting create dbentityentry<t>
through reflection testing purposes. constructor internal, phone call , pass in entity:
public dbentityentry<t> entry<t>(t entity) t : class { type constructedtype = typeof(dbentityentry<t>); bindingflags flags = bindingflags.nonpublic | bindingflags.instance; object x = activator.createinstance(constructedtype, flags, null, new object[] { entity }, null);
i get:
constructor on type 'system.data.entity.infrastructure.dbentityentry`1[[system.object, mscorlib, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089]]' not found.
the source constructor is:
internal dbentityentry(internalentityentry internalentityentry) { debugcheck.notnull(internalentityentry); _internalentityentry = internalentityentry; }
any way create instance?
update:
the original code lifted 1 of marc gravell's previous answers, since post candidate beingness close, provide seek using gravell's reply in suggestion:
type[] argtypes = new type[] { typeof(t) }; object[] argvalues = new object[] { entity }; bindingflags flags = bindingflags.nonpublic | bindingflags.instance; constructorinfo ctor = typeof (dbentityentry<t>).getconstructor(flags, null, callingconventions.any, argtypes, null); object obj = ctor.invoke(argvalues);
ctor
null, constructor 1 time again not found.
try using this.
public dbentityentry<t> entry<t>(t entity) t : class { constructorinfo constructor = null; seek { // binding flags exclude public constructors. constructor = typeof(dbentityentry<t>) .getconstructor(bindingflags.instance | bindingflags.nonpublic, null, new type[] { typeof(internalentityentry) }, null); } grab (exception exception) { throw new exception("error finding constructor", exception); } if (constructor == null) // || constructor.isassembly) // exclude internal constructors ... note, including illustration throw new exception(string.format("a private or " + "protected constructor missing '{0}'.", typeof(dbentityentry<t>).name)); homecoming (dbentityentry<t>)constructor.invoke(new object[] { entity }); }
c# entity-framework
No comments:
Post a Comment