I'm doing custom projections on Linq queries in Medium Trust and I get a MethodAccessException
or TypeAccessException
complaining about reflection and security rights.
I've simplified the code to the following:
var anon1 = new { Name = "Bill Gates" };
var ctor = anon1.GetType().GetConstructors().First();
// With native Reflection it works
var anon2 = ctor.Invoke(new object[] { "Steve Ballmer" });
var expr = Expression.New(ctor, Expression.Constant("Scott Guthrie"));
var lamb = Expression.Lambda(expr); // This throws in Medium Trust
var anon3 = lamb.Compile().DynamicInvoke();
anon1.ToString(); // --> { Name = Bill Gates }
anon2.ToString(); // --> { Name = Steve Ballmer }
anon3.ToString(); // --> { Name = Scott Guthrie }
In Full Trust, anon2
and anon3
will be created. In Medium Trust only anon2
will be created.
Another similar situation did not solve the issue