Skip to content

Commit

Permalink
Merge pull request #4 from cbersch/NestedPublic
Browse files Browse the repository at this point in the history
Add support for nested public classes
  • Loading branch information
NdubuisiJr authored Apr 26, 2024
2 parents f434c2c + bc84eac commit 1d15d21
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
29 changes: 29 additions & 0 deletions TypeExtender/TypeExtender.Test/TypeExtenderShould.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,5 +277,34 @@ public void AddFieldWithAttributes() {
Assert.NotNull(attributeC);
Assert.That(attributeC, Has.Property("Name").EqualTo("Tyrion Lannister"));
}

[Test]
public void ThrowWhenBaseTypeIsNotPublic()
{
Assert.Throws<ArgumentException>(() => new TypeExtender("ClassA", typeof(TestInternalClass)));
}

[Test]
public void ThrowWhenBaseTypeIsSealed()
{
Assert.Throws<ArgumentException>(() => new TypeExtender("ClassA", typeof(TestSealedClass)));
}

[Test]
public void SucceedWhenInstantiatingNestedPublicClass()
{
_typeExtender = new TypeExtender("ClassA", typeof(TestNestedPublicClass));
var returnedClass = _typeExtender.FetchType();

var instance = Activator.CreateInstance(returnedClass);

Assert.IsNotNull(instance);
}

public class TestNestedPublicClass { }
}

internal class TestInternalClass { }

public sealed class TestSealedClass { }
}
2 changes: 1 addition & 1 deletion TypeExtender/TypeExtender/TypeExtender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ private void generateGetter(string propertyName, FieldBuilder field, PropertyBui
}

private void initializeTypeConstruction() {
if (!BaseType.Attributes.HasFlag(TypeAttributes.Public) || BaseType.Attributes.HasFlag(TypeAttributes.Sealed)) {
if (BaseType.Attributes.HasFlag(TypeAttributes.Sealed) || !BaseType.Attributes.HasFlag(TypeAttributes.Public) && !BaseType.Attributes.HasFlag(TypeAttributes.NestedPublic)) {
throw new ArgumentException($"{BaseType} is either sealed or not public");
}

Expand Down

0 comments on commit 1d15d21

Please sign in to comment.