1

Closed

TypeLoadException thrown for a class nested in an internal class

description

internal class Program
{
    private static void Main(string[] args)
    {
        Database.SetInitializer(new DropCreateDatabaseAlways<StoreContext>());

            using (var context = new StoreContext())
            {
                context.Products.ToList();
            }
    }

    public class StoreContext : DbContext
    {
        public virtual DbSet<Product> Products { get; set; }
    }

    public class Product
    {
        public virtual int Id { get; set; }
        public virtual string Name { get; set; }
    }
}
Unhandled Exception: System.Reflection.TargetInvocationException: Exception has
been thrown by the target of an invocation. ---> System.TypeLoadException: Acces
s is denied: 'ConsoleApplication.Program+Product'.
at System.Reflection.Emit.TypeBuilder.TermCreateClass(RuntimeModule module, I
nt32 tk, ObjectHandleOnStack type)
at System.Reflection.Emit.TypeBuilder.CreateTypeNoLock()
at System.Reflection.Emit.TypeBuilder.CreateType()
at System.Data.Entity.Core.Objects.Internal.EntityProxyFactory.ProxyTypeBuild
er.CreateType(ModuleBuilder moduleBuilder) in c:\enlistments\OpenEF\src\EntityFr
amework\Core\Objects\Internal\EntityProxyFactory.cs:line 680
at System.Data.Entity.Core.Objects.Internal.EntityProxyFactory.BuildType(Modu
leBuilder moduleBuilder, ClrEntityType ospaceEntityType, MetadataWorkspace works
pace) in c:\enlistments\OpenEF\src\EntityFramework\Core\Objects\Internal\EntityP
roxyFactory.cs:line 382
at System.Data.Entity.Core.Objects.Internal.EntityProxyFactory.TryCreateProxy
Type(EntityType ospaceEntityType, MetadataWorkspace workspace) in c:\enlistments
\OpenEF\src\EntityFramework\Core\Objects\Internal\EntityProxyFactory.cs:line 237

at System.Data.Entity.Core.Objects.Internal.EntityProxyFactory.GetProxyType(C
lrEntityType ospaceEntityType, MetadataWorkspace workspace) in c:\enlistments\Op
enEF\src\EntityFramework\Core\Objects\Internal\EntityProxyFactory.cs:line 156
at System.Data.Entity.Core.Common.Internal.Materialization.Translator.Transla
torVisitor.Visit(EntityColumnMap columnMap, TranslatorArg arg) in c:\enlistments
\OpenEF\src\EntityFramework\Core\Common\internal\materialization\translator.cs:l
ine 275
at System.Data.Entity.Core.Query.InternalTrees.EntityColumnMap.Accept[TResult
Type,TArgType](ColumnMapVisitorWithResults2 visitor, TArgType arg) in c:\enlist
ments\OpenEF\src\EntityFramework\Core\Query\InternalTrees\EntityColumnMap.cs:lin
e 62
at System.Data.Entity.Core.Common.Internal.Materialization.Translator.Transla
torVisitor.ProcessCollectionColumnMap(CollectionColumnMap columnMap, TranslatorA
rg arg, ColumnMap discriminatorColumnMap, Object discriminatorValue) in c:\enlis
tments\OpenEF\src\EntityFramework\Core\Common\internal\materialization\translato
r.cs:line 888
at System.Data.Entity.Core.Common.Internal.Materialization.Translator.Transla
torVisitor.ProcessCollectionColumnMap(CollectionColumnMap columnMap, TranslatorA
rg arg) in c:\enlistments\OpenEF\src\EntityFramework\Core\Common\internal\materi
alization\translator.cs:line 849
at System.Data.Entity.Core.Common.Internal.Materialization.Translator.Transla
torVisitor.Visit(SimpleCollectionColumnMap columnMap, TranslatorArg arg) in c:\e
nlistments\OpenEF\src\EntityFramework\Core\Common\internal\materialization\trans
lator.cs:line 833
at System.Data.Entity.Core.Query.InternalTrees.SimpleCollectionColumnMap.Acce
pt[TResultType,TArgType](ColumnMapVisitorWithResults
2 visitor, TArgType arg) in
c:\enlistments\OpenEF\src\EntityFramework\Core\Query\InternalTrees\SimpleCollec
tionColumnMap.cs:line 53
at System.Data.Entity.Core.Common.Internal.Materialization.Translator.Transla
teColumnMap[T](QueryCacheManager queryCacheManager, ColumnMap columnMap, Metadat
aWorkspace workspace, SpanIndex spanIndex, MergeOption mergeOption, Boolean valu
eLayer) in c:\enlistments\OpenEF\src\EntityFramework\Core\Common\internal\materi
alization\translator.cs:line 62
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments,
Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Objec
t[] parameters, Object[] arguments)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invoke
Attr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
at System.Data.Entity.Core.Common.Internal.Materialization.Translator.Transla
teColumnMap(Translator translator, Type elementType, QueryCacheManager queryCach
eManager, ColumnMap columnMap, MetadataWorkspace workspace, SpanIndex spanIndex,
MergeOption mergeOption, Boolean valueLayer) in c:\enlistments\OpenEF\src\Entit
yFramework\Core\Common\internal\materialization\translator.cs:line 104
at System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlanFactory.P
repare(ObjectContext context, DbQueryCommandTree tree, Type elementType, MergeOp
tion mergeOption, Boolean streaming, Span span, IEnumerable1 compiledQueryParam
eters, AliasGenerator aliasGenerator) in c:\enlistments\OpenEF\src\EntityFramewo
rk\Core\Objects\Internal\ObjectQueryExecutionPlanFactory.cs:line 47
at System.Data.Entity.Core.Objects.EntitySqlQueryState.GetExecutionPlan(Nulla
ble
1 forMergeOption) in c:\enlistments\OpenEF\src\EntityFramework\Core\Objects\
Internal\EntitySqlQueryState.cs:line 190
at System.Data.Entity.Core.Objects.ObjectQuery1.<>c__DisplayClassb.<GetResul
ts>b__a() in c:\enlistments\OpenEF\src\EntityFramework\Core\Objects\ObjectQuery

.cs:line 722
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func
1 func, Boolean throwOnExistingTransaction, Boolean startLocalTransaction, Bool
ean releaseConnectionOnSuccess) in c:\enlistments\OpenEF\src\EntityFramework\Cor
e\Objects\ObjectContext.cs:line 2979
at System.Data.Entity.Core.Objects.ObjectQuery
1.<>c__DisplayClassb.<GetResul
ts>b__9() in c:\enlistments\OpenEF\src\EntityFramework\Core\Objects\ObjectQuery.cs:line 721
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](
Func
1 func) in c:\enlistments\OpenEF\src\EntityFramework.SqlServer\DefaultSqlEx
ecutionStrategy.cs:line 54
at System.Data.Entity.Core.Objects.ObjectQuery1.GetResults(Nullable1 forMer
geOption) in c:\enlistments\OpenEF\src\EntityFramework\Core\Objects\ObjectQuery.cs:line 720
at System.Data.Entity.Core.Objects.ObjectQuery
1.<System.Collections.Generic.
IEnumerable<T>.GetEnumerator>b__0() in c:\enlistments\OpenEF\src\EntityFramework
\Core\Objects\ObjectQuery.cs:line 554
at System.Lazy
1.CreateValue()
at System.Lazy1.LazyInitValue()
at System.Lazy
1.get_Value()
at System.Data.Entity.Internal.LazyEnumerator1.MoveNext() in c:\enlistments\
OpenEF\src\EntityFramework\Internal\LazyEnumerator
.cs:line 38
at System.Collections.Generic.List1..ctor(IEnumerable1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at ConsoleApplication.Program.Main(String[] args) in c:\Users\ansvyryd\Doc
uments\Visual Studio 2012\Projects\ConsoleApplication\ConsoleApplication\P
rogram.cs:line 14
Closed Oct 31, 2013 at 6:38 PM by lukew
Verified in 6.0.2 branch

comments

ajcvickers wrote Aug 12, 2013 at 4:44 PM

Fixed in 85d7cfdf7dbb

Your Public Image is Limited (Fix for 997: TypeLoadException thrown for a class nested in an internal class)

This was an issue with proxy creation of nested types. The proxy factory would check the visibility of the class in order to determine whether or not a new class in a different assembly is able to derive from it. Given a nested class like so:

internal class Outer
{
public class Inner
{
}
}

Reflection returned the visibility of Inner as "PublicNested". We were therefore trying to derive from it to create the proxy. But even though Reflection says the class is public for all intents and purpose it is actually internal since it is nested inside an internal class. Therefore we cannot derive from it.

The fix is to correctly detect, despite what Reflection says, that types nested inside non-public types are not, practically speaking, public. We therefore do not attempt to create proxies for them.

lukew wrote Oct 17, 2013 at 11:19 PM

Verified, the sample code no longer repro's and tests added

** Closed by lukew 10/17/2013 4:19PM

ajcvickers wrote Oct 30, 2013 at 9:40 PM

This has been merged into the 6.0.2 branch and is now ready to verify in that branch.