ReflectionReadOnlyDictionaryBuilder
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 ReflectionReadOnlyDictionaryBuilder : ModelReaderWriterTypeBuilder
{
private Type _dictionaryType;
protected override Type BuilderType => typeof(Dictionary<, >).MakeGenericType(_dictionaryType.GenericTypeArguments);
[System.Runtime.CompilerServices.Nullable(2)]
protected override Type ItemType {
[System.Runtime.CompilerServices.NullableContext(2)]
get {
return _dictionaryType.GetGenericArguments()[1];
}
}
public ReflectionReadOnlyDictionaryBuilder(Type dictionaryType)
{
_dictionaryType = dictionaryType;
}
protected override object CreateInstance()
{
return Activator.CreateInstance(BuilderType);
}
protected override void AddItemWithKey(object dictionary, string key, [System.Runtime.CompilerServices.Nullable(2)] object item)
{
BuilderType.GetMethod("Add", new Type[2] {
typeof(string),
ItemType
}).Invoke(dictionary, new object[2] {
key,
item
});
}
protected override object ConvertCollectionBuilder(object builder)
{
return Activator.CreateInstance(_dictionaryType, builder);
}
}
}