By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,853 Members | 1,027 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,853 IT Pros & Developers. It's quick & easy.

Passing references to CodeDom

P: 9
Hello,

I was wondering if anyone could tell me how I can pass a reference of an object(lets pretend a MemoryStream) to CodeDom's generated code in memory?

So basically I would like to create CodeDom code that gets a reference as a parameter to an object in Form1. Then I would like to call the CodeDom code with Reflection, and have CodeDom access the object reference that was passed as a parameter. Thusly in my CodeDom code, I could have "memorystream.WriteByte(44)", and then back in Form1 I could see that memorystream has a new byte written to it...

Thanks...
Oct 22 '08 #1
Share this Question
Share on Google+
1 Reply


P: 9
Nevermind, got it:

If you set up a barebones CodeDom configuation, using this....

List<string> stringlist = new List<string>();

public CodeNamespace myNamespace = new CodeNamespace("MyNamespace");
public CodeTypeDeclaration myFunctions = new CodeTypeDeclaration("MyFunctions");

public string code = @"public static string StringTest(System.Collections.Generic.List<string> mystrings)

mem = new CodeSnippetTypeMember(code);
myFunctions.Members.Add(mem);
myNamespace.Types.Add(myFunctions);


Then create a method for Reflection like this:

Type t = results.CompiledAssembly.GetType("MyNamespace.MyFu nctions");
System.Reflection.MethodInfo info = t.GetMethod("StringTest");
object o;
object[] objarray = new object[1]; //Could substitue 1 with stringlist.Count
objarray[0] = stringlist; // Could use for loop here to write out stringlist[x] to objarray[y]

o = info.Invoke(null, objarray);
string test = o.ToString();// o is holding the result of whatever happend with Invoke on "StringTest"... which can be whatever you want!
Oct 22 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.