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?
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
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;
}
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.