CodeDom Question


J

Jeff Benson

Hello,

I am wondering if it is possible to create a dynamically synthesized
assembly (one that I'm compiling on-the-fly from C# code I've strung
together myself at runtime) using the System.CodeDom.Compiler namespace, and
give the resulting assembly identical permissions as those of the current,
running assembly (or even be given the ability to access fields declared as
"internal", etc.)

Specifically, code in Assembly A wants to dynamically compile a new Assembly
B (using the System.CodeDom.Compiler namespace) and allow the just-compiled
Assembly B to access private fields in Assembly A. (I could probably make
them "protected" or "internal" if I had to, but would prefer to let them
remain private.)

I'm trying to fabricate data-mapping objects on the fly, based on metadata,
and it's working perfectly except for the fact that the new objects don't
have permission to set (or read) the private fields on the business objects
they're supposed to be mapping to a database. If I made all of these fields
public, the technique would work perfectly, but I'd prefer not to break
encapsulation in that way.

Any help would be tremendously, hugely appreciated!

Jeff Benson
(e-mail address removed)
 
Ad

Advertisements

D

Dino Chiesa [Microsoft]

if you don't like protected members, then use Reflection.
In the c# you "string together" and later compile, use Reflection to invoke
the private members of A.


using System.Reflection;

class A {
private static int SomeSecretNumber = 42;
}

class Test1 {
static void Main() {
System.Reflection.Assembly a=
System.Reflection.Assembly.GetExecutingAssembly();
System.Type t = a.GetType("A", false, true);
try {
FieldInfo fi = t.GetField("SomeSecretNumber", BindingFlags.NonPublic |
BindingFlags.Static);
System.Console.WriteLine("The secret number is {0}.",
fi.GetValue(null));
}
catch (System.Exception ex) {
System.Console.WriteLine(ex);
}
}
}


Lots of examples of how to use reflection to invoke private members.
Here's a good one with an example and some commentary
http://blogs.gotdotnet.com/gregfee/categoryview.aspx/Reflection


Your generated and dynamically-compiled code ought to be able to reflect on
its caller and access private members.

-Dino
 
J

Jeff Benson

Thanks very much for the response!

I already have a reflection-based approach that works, but I'm trying to
avoid the performance implications. Reflection doesn't seem like it's the
fastest approach at runtime, so I was trying to autogenerate a class that
knew how to do what needed to be done in the most direct possible fashion
(while not hardcoding the schema).

Or maybe I'm suffering from a misperception? Are there performance
penalties associated with a reflection-based approach?

Jeff Benson
(e-mail address removed)
 
Ad

Advertisements

M

Mountain Bikn' Guy

Yes, reflection has performance penalties, so I think your perceptions are
correct. If you find a solution, please post a note to the group because I'd
like to know about a non-reflection-base solution as well.
Dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

CodeDOM question 0
CodeDOM question 0
CodeDom and @ 0
slow CodeDom 0
EnvDTE of CodeDom 1
CodeDom and enums 0
Doing "&&" in codeDOM 2
CodeDOM question: explicit conversion operator generation 1

Top