Reflection and winforms

  • Thread starter Thread starter raulavi
  • Start date Start date
R

raulavi

HI:
how do I display a form after created the class containing the winform ?
I have a project creating a class with c# code lines to create controls for
a winform.
(the project uses reflection to get all the data to create the controls.)
I save the winform class into a file (classfilename.cs)
after I saved the classfilename.cs, I would like to show the winform

Can anyone tell me the steps. please



Thanks
 
MyFormClass theForm = new MyFormClass();
theForm.Show();

unless I am missing something painfully obvious to someone else...
 
how do I display a form after created the class containing the winform ?
I have a project creating a class with c# code lines to create controls
for
a winform.
(the project uses reflection to get all the data to create the controls.)
I save the winform class into a file (classfilename.cs)
after I saved the classfilename.cs, I would like to show the winform

Can anyone tell me the steps. please

You can't create an object simply from source code, as C# is a compiled
language, not an interpreted one. You'd need to compile classfilename.cs
into a separate assembly and then access the form from there. The only other
option I know of is Reflection.Emit to create an assembly on the fly, and
that's not a trivial exercise.
 
I guess I was not too clear on my request ...thanks rhaazy.

the form is created on a source file as of classnamefile.cs
it is not compiled with the reflection project...
(this may involve serialization or so)


your answer is ok when a form class exist and it is compiled with your
project.
 
Thanks Johnson:
if the file classfilename.cs is serialized ?

lets say on reflection proj I have a dummy winform lets call it winformDummy
(the layout of this dummy form won't matter,since will be replaced by the
created classfilename.cs)
could this be done?

this is just a concept, I dont have the correct idea yet.
 
Thanks Johnson:
if the file classfilename.cs is serialized ?

I'm assuming classfilename.cs contains plain old C# source code. In that
case, it's not "serialized" in any sense other than the fact that it's
written to disk. There is no built-in deserialization routine for source
code.
lets say on reflection proj I have a dummy winform lets call it
winformDummy
(the layout of this dummy form won't matter,since will be replaced by the
created classfilename.cs)
could this be done?

If all you're looking to do is create controls which will be wired to
EXISTING code, then something can be done, but you'd need to implement your
own data structures to identify these controls and their properties.

If, however, these controls need to provider their OWN code, then you've got
a tougher situation. There is a namespace I didn't mention in my first reply
called System.CodeDom. I've never used it myself, but I believe it can be
used to take source code and generate assemblies. You might want to look
into it. However, I might have it completely backwards: it might take
executable code and turn it into source code, rather than taking source code
and turning it into executable code. (Maybe System.CodeDom.Compiler can
handle that part.)
 
raulavi said:
Thanks Johnson:
if the file classfilename.cs is serialized ?

lets say on reflection proj I have a dummy winform lets call it
winformDummy
(the layout of this dummy form won't matter,since will be replaced by the
created classfilename.cs)
could this be done?

this is just a concept, I dont have the correct idea yet.


Reflection, Serialization and Source Code are three very different concepts.
You could compile classfilename.cs to an assembly using reflection. You
could instantiate an object after it is compiled to an assembly. I'm not
sure what winformDummy would be necessary for, as presumably for this to
work, classfilename derives from System.Windows.Forms.Form, and instantiates
everything within it's code and not a resource file. I'm not even sure if
you expected to use the winformDummy layout for the classfilename.cs, and
the namespaces and classnames were different, whether the compiler would
allow them to work together.

I've got to believe at this point you might need to revisit your apps
requirements.
 
I always learn something new...thanks


this is the concept of my proj

I will read an exe using reflection
out of certain classes I will create for each property a control for the
winform
the winform is just the text saved as classfilename.cs
for ea class I will have a winform
while doing this I want to display the winform.
(i will display the classfilename.cs to view the created code for the
winform).

if I manually paste the code created on classfilename.cs into a screen
winform
i can see the screen design layout that is ok)...since there are so many
classes/winforms i would like to automize this step.
 
raulavi said:
I always learn something new...thanks


this is the concept of my proj

I will read an exe using reflection
out of certain classes I will create for each property a control for the
winform

I understand (I think) so far...
the winform is just the text saved as classfilename.cs

Well, no, if you have a winform, you would normally have a code file, and a
resource file. You say this is "just the text saved as classfilename.cs".
Is it compileable code or is it just the property names you got from the
class in the reflected exe above? Maybe an example of what you are
expecting to put into classfilename.cs will help.
for ea class I will have a winform
while doing this I want to display the winform.
(i will display the classfilename.cs to view the created code for the
winform).

if I manually paste the code created on classfilename.cs into a screen
winform
i can see the screen design layout that is ok)...since there are so many
classes/winforms i would like to automize this step.

You pasted text into the blank form? This does not work for me, because you
would need to paste a control into a form in Visual Studio. Again, maybe
your example of a clasfilename.cs will help to understand this.
 
Family Tree Mike sorry got sidetrack...

the classfilename.cs is exactly created as when you use the VS to create a a
winform. every single line is in this file classfilename.cs,

there will be another project getting these files and adding to the real
project for the end-user. these classes will be compiled then.


I just need to see hoiw the winform look after classfilename.cs is created
using the reflection (here is the "how" that I dont know) .
 
raulavi said:
Family Tree Mike sorry got sidetrack...

the classfilename.cs is exactly created as when you use the VS to create a
a
winform. every single line is in this file classfilename.cs,

there will be another project getting these files and adding to the real
project for the end-user. these classes will be compiled then.


I just need to see hoiw the winform look after classfilename.cs is
created
using the reflection (here is the "how" that I dont know) .


Well, you can compile the class to an assembly from your class. You can
load the class and then create an instance and call the .Show() method
because it derives from System.Windows.Forms.Form.
 
sounds good...
(at runtime correct?) compile the created class source ??? hum, any idea of
a site where I can read about it how?

thanks
 
raulavi said:
sounds good...
(at runtime correct?) compile the created class source ??? hum, any idea
of
a site where I can read about it how?

thanks


Yes, at runtime. As Jeff alluded to earlier, there is Reflection.Emit and
there is also CodeDom. Here are a couple of links to look at, but there is
plenty of info on each in google...

http://ayende.com/Blog/archive/2006/08/13/ReflectionEmitVsCodeDOM.aspx
http://msdn.microsoft.com/en-us/library/3y322t50.aspx
http://www.dotnetspider.com/resources/565-Generating-C-Class-File-Sample-cs-Dynamically-us.aspx
 
Back
Top