This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ActivatableEntity : EntityBase | |
{ | |
public bool IsActive { get; set; } | |
public void Active() | |
{ | |
//... | |
} | |
public void DeActivate() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[MData("Customer")] | |
public interface ICustomer | |
{ | |
int Id { get; set; } | |
string Name { get; set; } | |
string Notes { get; set; } | |
bool IsActive { get; set; } | |
DateTime CreatedOn { get; set; } | |
DateTime ModifiedOn { get; set; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void MapInterfaceMethods(FieldInfo classToWrapField, TypeBuilderHelper typeBuilder) | |
{ | |
//loop al interface methods | |
foreach (var methodInfo in InterfaceToImplement.GetMethods()) | |
{ | |
//define the method in the generated type | |
var emitter = typeBuilder.DefineMethod(methodInfo) | |
.Emitter | |
//this.[fieldName] | |
.ldarg_0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void DefineConstructor(FieldInfo classToWrapField, TypeBuilderHelper typeBuilder) | |
{ | |
typeBuilder | |
//take an instance of 'ClassToWrap' as constructor parameter | |
.DefinePublicConstructor(ClassToWrap) | |
.Emitter | |
//: base() | |
.ldarg_0 | |
.call(typeof(object).GetConstructors()[0]) | |
//this.[fieldName] = [constructorParameter] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var typeBuilder = _assemblyBuilder.DefineType(GetGeneratedTypeName(), typeof(object), InterfaceToImplement); | |
//create private field | |
var classToWrapField = typeBuilder.DefineField(GetGeneratedToWrapFieldName(), ClassToWrap, FieldAttributes.Private); | |
//map interface members | |
MapInterfaceProperties(classToWrapField, typeBuilder); | |
MapInterfaceEvents(classToWrapField, typeBuilder); | |
MapInterfaceMethods(classToWrapField, typeBuilder); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static ClassFactory() | |
{ | |
_assemblyBuilder = new AssemblyBuilderHelper(GetGeneratedAssemblyPath()); | |
} | |
private static string GetGeneratedAssemblyPath() | |
{ | |
return @".\Duck.Tape.Generated.dll"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
A a = new A(); | |
ITest aAsITest = a.Duck<ITest>(); | |
} | |
} | |
public class A |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
EmitHelper emit = new AssemblyBuilderHelper("HelloWorld.dll") | |
.DefineType ("Hello", typeof(object), typeof(IHello)) | |
.DefineMethod(typeof(IHello).GetMethod("SayHello")) | |
.Emitter; | |
emit | |
// string.Format("Hello, {0}!", toWhom) | |
// | |
.ldstr ("Hello, {0}!") | |
.ldarg_1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static string Format(string format, object arg0, object arg1) | |
{ | |
return Format(null, format, new object[] { arg0, arg1 }); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Offset OpCode | |
0 ldnull | |
1 ldarg.0 | |
2 ldc.i4.2 | |
3 newarr System.Object | |
8 stloc.0 | |
9 ldloc.0 | |
10 ldc.i4.0 | |
11 ldarg.1 | |
12 stelem.ref |