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'ing 完成的。 http://www.postsharp.net/aspects/examples/inotifypropertychanged

希望这可以帮助

于 2013-07-02T17:31:08.690 回答
0

也许这样的事情会起作用:

public T Create<T>(bool createSubObjects)
    {
        T MyICustomer = default(T);
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.Append(@"
            using System;
            using System.Collections.Generic;
            using System.Linq;

            namespace MyNameSpace
            {
                public class RuntimeCustomer:NotifyPropertyChanged,").Append(typeof(T).FullName).Append(@"
                {
                    string name;").Append(createSubObjects ? @"
                    IAddress address = new RuntimeAddress(); " :@"
                    IAddress address = null;").Append(@"

                    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) }
                    }
                }
             }");

        Dictionary<string, string> providerOptions = new Dictionary<string, string>();
        providerOptions["CompilerVersion"] = "v3.5"; //OR YOUR VERSION
        Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider(providerOptions);

        System.CodeDom.Compiler.CompilerParameters parameters = new System.CodeDom.Compiler.CompilerParameters();
        parameters.GenerateExecutable = false;
        parameters.GenerateInMemory = true;
        parameters.IncludeDebugInformation = true;
        parameters.ReferencedAssemblies.Add("System.dll");
        parameters.ReferencedAssemblies.Add(typeof(System.Linq.Enumerable).Assembly.Location);
        parameters.ReferencedAssemblies.Add(System.Reflection.Assembly.GetCallingAssembly().Location);
        parameters.ReferencedAssemblies.Add(System.Reflection.Assembly.GetExecutingAssembly().Location);

        System.CodeDom.Compiler.CompilerResults results = provider.CompileAssemblyFromSource(parameters, sb.ToString());

        if (results.Errors.Count == 0)
        {
            Type generated = results.CompiledAssembly.GetType("MyNameSpace.RuntimeAddress");
            MyICustomer = (T)generated.GetConstructor(Type.EmptyTypes).Invoke(null);
        }
        else
        {
          //Do something
        }

        return MyICustomer; 
    }
于 2013-07-02T11:37:41.677 回答
0

作为替代方案,您可以使用 Fody。它将完成所有这些以及更多操作,而无需发出您自己的 IL。请参阅https://github.com/Fody/PropertyChanged 只需 Nuget 并将其添加到您的类中,或从 INotifyPropertyChanged 派生。

于 2013-07-02T13:33:27.390 回答