Skip to content

TypeBuilder::CreateTypeInfo() should not throw COMException #43282

@marek-safar

Description

@marek-safar

When TypeBuilder encounters by-ref field it throws COMException. This seems to be the only reference to COMException and the behaviour is not consistent with other error handling for different type members.

public void DefineField_ByRefFieldType_ThrowsCOMExceptionOnCreation()
{
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public);
type.DefineField("Name", typeof(int).MakeByRefType(), FieldAttributes.Public);
Assert.Throws<COMException>(() => type.CreateTypeInfo());
}

It should be replaced with ArgumentException as it's done for all other type members. Here is an example with events

[Fact]
public void DefineEvent_ByRefEventType_ThrowsArgumentException()
{
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public);
AssertExtensions.Throws<ArgumentException>(null, () => type.DefineEvent("Name", EventAttributes.None, typeof(int).MakeByRefType()));
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions