Skip to content

Commit

Permalink
Code cleanup, fewer magic strings
Browse files Browse the repository at this point in the history
  • Loading branch information
onionhammer committed Sep 12, 2015
1 parent 010b1a5 commit 9ce0e1f
Showing 1 changed file with 10 additions and 24 deletions.
34 changes: 10 additions & 24 deletions Wivuu.DataSeed/ILSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,44 +31,30 @@ public static T Compile<T>(Expression<T> expression)
where T : class
{
var typeBuilder = _assemblyModule.DefineType(
$"{expression.Name}op{Guid.NewGuid().ToString("N")}");
$"{typeof(T).Name}op{StringId()}");

var methodBuilder = typeBuilder.DefineMethod(
"Operation", MethodAttributes.Public | MethodAttributes.Static);
MethodBuilder operation;
operation = typeBuilder.DefineMethod(
nameof(operation), MethodAttributes.Public | MethodAttributes.Static);

expression.CompileToMethod(methodBuilder);
expression.CompileToMethod(operation);

return Delegate.CreateDelegate(
expression.Type,
typeBuilder.CreateType().GetMethod("Operation")
typeBuilder.CreateType().GetMethod(nameof(operation))
) as T;
}

static ModuleBuilder CreateModule() =>
// Define dynamic assembly
AppDomain.CurrentDomain.DefineDynamicAssembly(
new AssemblyName(nameof(ILSerializer) + Guid.NewGuid().ToString("N")),
new AssemblyName(nameof(ILSerializer) + StringId()),
AssemblyBuilderAccess.RunAndSave
).DefineDynamicModule("Module");

static Delegate CompileToType(
this ModuleBuilder module,
Type type, LambdaExpression expression)
{
var typeBuilder = module.DefineType(
type.Name + "Operation" + Guid.NewGuid().ToString("N"));

var methodBuilder = typeBuilder.DefineMethod(
"Operation", MethodAttributes.Public | MethodAttributes.Static);

expression.CompileToMethod(methodBuilder);

return Delegate.CreateDelegate(
expression.Type,
typeBuilder.CreateType().GetMethod("Operation")
);
}
static string StringId() =>
Guid.NewGuid().ToString("N").Substring(0, 6);

#endregion
}
}
}

0 comments on commit 9ce0e1f

Please sign in to comment.