1

以下に示すいくつかの追加機能を使用して、実行時にインターフェイスの実装を生成できるライブラリがあるかどうかを尋ねたいと思います。

私がそのようなインターフェースを持っているとしましょう:

interface ICustomer
{
    string Name {get;set;}
    string IAddress { get;set; }
}

interface IAddress
{
    string Street {get;set;}
}

私はそのようなことをしたいと思います:

ICustomer customer = someLibrary.Create<ICustomer>(bool createSubObjects)

whereCreate<T>()メソッドは、実行時に次のような実装を作成します。

class RuntimeCustomer : NotifyPropertyChanged,ICustomer 
//NotifyPropertyChanged would be hand written class
{
    string name;
    IAddress address = new RuntimeAddress(); 
    //if createSubObjects == false then `IAddress address = null`

    public string Name 
    {
       get { return name; }
       set { SetProperty(ref name, value); }
    }
    public IAddress Address
    { 
       get { return address; }
       set { SetProperty(ref address, value) }
    }
}

class RuntimeAddress : NotifyPropertyChanged, IAddress
{
    string street;
    public string Street 
    {
        get { return street; }
        set { SetProperty(ref,street, value) }
    }
}

何かアイデアはありますか?

4

3 に答える 3

3

jureが言ったように、これにはDynamicProxyを使用できますが、それはインターフェースのJIT実装に適用される側面になります。これをチェックしてください: http://jonas.follesoe.no/2009/12/23/automatic-inotifypropertychanged-using-dynamic-proxy/

PostSharp はこのユースケースにも優れていますが、コンパイル時のウィービングと実行時の JIT によって行われます。 http://www.postsharp.net/aspects/examples/inotifypropertychanged

お役に立てれば

于 2013-07-02T17:31:08.690 に答える
0

別の方法として、Fody を使用することもできます。独自の IL を発行する必要なく、これらすべてを実行できます。https://github.com/Fody/PropertyChangedを参照してください。 単純に Nuget して、クラスに属性を追加するか、INotifyPropertyChanged から派生させます。

于 2013-07-02T13:33:27.390 に答える