これはあなたのシナリオを正確に扱っているわけではありませんが、オブジェクトの周りのデコレータとして機能する DynamicObject 実装を使用するのはどうですか? 元のオブジェクトと追加のプロパティにアクセスできます。ExpandoObject に似ていますが、独自のインスタンス データから始めます。
このようなもの:
public class Expando : DynamicObject
{
public dynamic Instance;
Dictionary<string, dynamic> ExtraProperties = new Dictionary<string, dynamic>();
public Expando(object instance)
{
Instance = instance;
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
try
{
result = ReflectionUtils.GetProperty(Instance, binder.Name);
return true;
}
catch
{
if (ExtraProperties.Keys.Contains(binder.Name))
{
result = ExtraProperties[binder.Name];
return true;
}
}
result = null;
return false;
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
try
{
ReflectionUtils.SetProperty(Instance, binder.Name, value);
}
catch (Exception ex)
{
ExtraProperties[binder.Name] = value;
}
return true;
}
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
{
try
{
result = ReflectionUtils.CallMethod(Instance, binder.Name, args);
return true;
}
catch
{}
result = null;
return false;
}
}
残念ながら、強力なタイピング/インターフェイスの要件には対応していません。また、ここでのリフレクションの使用を考えると、パフォーマンスが最大になることはありません ( https://github.com/RickStrahl/Westwind.Utilities/blob/master/Westwind.ユーティリティ/ユーティリティ/ReflectionUtils.cs )