If I'm creating a dynamic type like so:
TypeBuilder dynaType = dynaModule.DefineType(typeof(T).Name + "_ORMProxy");
dynaType.AddInterfaceImplementation(typeof(IServiceTable));
// (1) Implement: (String) IServiceTable.TableName { get; }
FieldBuilder tableNameField = dynaType.DefineField("tableName", typeof(String), FieldAttributes.Private);
MethodBuilder tableNamePublicGetAccessor = dynaType.DefineMethod("get_tableName", MethodAttributes.Public);
tableNamePublicGetAccessor...
Is it possible to set the GetAccessor
method to an expression tree. They're much easier to work with than straight IL.
Expression Trees represent code as a structure that you can examine, modify, or execute. These tools give you the power to manipulate code during run time. You can write code that examines running algorithms, or injects new capabilities.
Expression Trees was first introduced in C# 3.0 (Visual Studio 2008), where they were mainly used by LINQ providers. Expression trees represent code in a tree-like format, where each node is an expression (for example, a method call or a binary operation such as x < y).
Each node in an expression tree is an expression. For example, an expression tree can be used to represent mathematical formula x < y where x, < and y will be represented as an expression and arranged in the tree like structure. Expression tree is an in-memory representation of a lambda expression.
Yes and no. The LambdaExpression.CompileToMethod()
method will let you compile an expression tree to a MethodBuilder
only if the method is static. It cannot be used to implement instance methods, which I believe is what you want in your example.
That's exactly what the LambdaExpression.CompileToMethod()
method is for.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With