ReflectionReadOnlyCollectionBuilder
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
namespace System.ClientModel.Primitives
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("This class uses reflection. Pass in a generated ModelReaderWriterContext to be AOT compatible.")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("This class uses reflection. Pass in a generated ModelReaderWriterContext to be AOT compatible.")]
internal class ReflectionReadOnlyCollectionBuilder : ModelReaderWriterTypeBuilder
{
private Type _collectionType;
protected override Type BuilderType => typeof(List<>).MakeGenericType(_collectionType.GenericTypeArguments);
protected override Type ItemType => _collectionType.GenericTypeArguments[0];
public ReflectionReadOnlyCollectionBuilder(Type collectionType)
{
_collectionType = collectionType;
}
protected override object CreateInstance()
{
return Activator.CreateInstance(BuilderType);
}
protected override void AddItem(object collection, [System.Runtime.CompilerServices.Nullable(2)] object item)
{
BuilderType.GetMethod("Add", new Type[1] {
ItemType
}).Invoke(collection, new object[1] {
item
});
}
protected override object ConvertCollectionBuilder(object builder)
{
return BuilderType.GetMethod("AsReadOnly").Invoke(builder, null);
}
}
}