Skip to content

The dynamic type created is thrown Fileloadexception when using the GetCustomAttributes method #26912

@joshfree

Description

@joshfree

Original issue dotnet/core#1481

@faib920 commented on Apr 26
I Use TypeBuilder create a testclass, and use SetCustomAttributes to add custom attribute, then I use the method of GetCustomAttributes, throw FileLoadFileException.

FileLoadException:
Could not load file or assembly '_testclass, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'. Operation is not supported. (Exception from HRESULT: 0x80131515)

inner Exception
System.NotSupportedException
Resolving to a collectible assembly is not supported.

StackTrace:

at System.Reflection.CustomAttribute._CreateCaObject(RuntimeModule pModule, IRuntimeMethodInfo pCtor, Byte** ppBlob, Byte* pEndBlob, Int32* pcNamedArgs)
at System.Reflection.CustomAttribute.CreateCaObject(RuntimeModule module, IRuntimeMethodInfo ctor, IntPtr& blob, IntPtr blobEnd, Int32& namedArgs)
at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean mustBeInheritable, IList derivedAttributes, Boolean isDecoratedTargetSecurityTransparent)
at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeType type, RuntimeType caType, Boolean inherit)
at System.Attribute.GetCustomAttributes(MemberInfo element, Type type, Boolean inherit)
at System.Reflection.CustomAttributeExtensions.GetCustomAttributes[T](MemberInfo element, Boolean inherit)

@evo01 commented 14 days ago
I did some more testing around this issue and the temporary work around seems to be to not use named arguments. If you leave the named arguments off, and use the following property instead, you can custom attributes in a dynamic type.

[Custom1("Howdy", new []{ EnumTesting_BasicEnum_Int16 .One})] 
public class ClassTesting_ClassWithBlankAttribute
{
}

var attributes = dynamicType.CustomAttributes.ToList(); // WORKS

Assert.AreEqual(1, attributes.Count); // WORKS

This does not work:

[Custom1("Howdy", new []{ EnumTesting_BasicEnum_Int16 .One}, MyField1 = new[] { EnumTesting_BasicEnum_Int16.One }, MyField2 = typeof(Custom1Attribute), MyProperty1 =1)]
public class ClassTesting_ClassWithBlankAttribute
{
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions