1

I would like to ask is there a library which allows to generate implementation of interface at runtime with some additional features illustrated below.

Let's say I have interface like that:

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

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

I would like to do something like that:

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

where Create<T>() method would create an implementation like this at runtime:

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

Any idea please?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
user2542183
  • 53
  • 2
  • 6

3 Answers3

3

as jure said you can use DynamicProxy for this, but it would be aspects applied to a JIT'ed implementation of your interfaces. Check this out: http://jonas.follesoe.no/2009/12/23/automatic-inotifypropertychanged-using-dynamic-proxy/

PostSharp excels at this use case as well, but it's done by compile-time weaving vs. runtime JIT'ing. http://www.postsharp.net/aspects/examples/inotifypropertychanged

Hope this helps

kellyb
  • 1,391
  • 1
  • 11
  • 18
0

Maybe something like this would work :

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; 
    }
sm_
  • 2,572
  • 2
  • 17
  • 34
  • thanks for the answer. I believe IL emitting or compiling like that is how it must be done and your code could work great however this interfaces where just samples so I thought some library doing that in generic way exists – user2542183 Jul 02 '13 at 11:50
  • @user2542183 Well you could create your own library, and make it more flexible by passing the code text to the create function as a parameter. Because the implementation of a certain interface has got to be defined somewhere. Up-vote or mark as answer :) ? – sm_ Jul 02 '13 at 11:54
  • oh yeah, I will eventually but I hoped something like that already exists – user2542183 Jul 02 '13 at 11:55
0

As an alternative you could you Fody. It will do all this and more without needing to emit your own IL. See https://github.com/Fody/PropertyChanged Simply Nuget it and add either an attribute to your class, or derive from INotifyPropertyChanged.

Marwijn
  • 1,681
  • 15
  • 24