473,385 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Dynamic Code

Is it possible to create dynamic code which is not part of a class ?

Apr 4 '07 #1
11 1714
On Apr 4, 2:17 pm, Roderick.Buhag...@gmail.com wrote:
Is it possible to create dynamic code which is not part of a class ?
Could you clarify what you mean?

Jon

Apr 4 '07 #2
Hello Roderick,

generally yes,

see: http://www.codeproject.com/useritems...eneration1.asp

Regards

Frank
Is it possible to create dynamic code which is not part of a class ?

Apr 4 '07 #3
On Apr 4, 3:23 pm, Frank Werner <frank.werner-krippend...@franke.com>
wrote:
Hello Roderick,

generally yes,

see:http://www.codeproject.com/useritems...eneration1.asp

Regards

Frank
Is it possible to create dynamic code which is not part of a class ?- Hide quoted text -

- Show quoted text -
but that creates an assembly....right ? is it possible to run it
directly ?

Apr 4 '07 #4
then this: http://www.codeproject.com/cs/algorithms/matheval.asp would fit
your needs
Regards

Frank
On Apr 4, 3:23 pm, Frank Werner <frank.werner-krippend...@franke.com>
wrote:
>Hello Roderick,

generally yes,

see:http://www.codeproject.com/useritems...Generation1.as
p

Regards

Frank
>>Is it possible to create dynamic code which is not part of a class
?- Hide quoted text -
- Show quoted text -
but that creates an assembly....right ? is it possible to run it
directly ?

Apr 4 '07 #5
On Apr 4, 3:54 pm, Frank Werner <frank.werner-krippend...@franke.com>
wrote:
then this:http://www.codeproject.com/cs/algori...heval.aspwould fit
your needs

Regards

Frank
On Apr 4, 3:23 pm, Frank Werner <frank.werner-krippend...@franke.com>
wrote:
Hello Roderick,
generally yes,
see:http://www.codeproject.com/useritems...Generation1.as
p
Regards
Frank
>Is it possible to create dynamic code which is not part of a class
?- Hide quoted text -
- Show quoted text -
but that creates an assembly....right ? is it possible to run it
directly ?- Hide quoted text -

- Show quoted text -
and is it possible to compile the code without putting it in a
class...example code = "Console.WriteLine("Hello");"

Apr 4 '07 #6
well, it is a string variable that holds the source - so you can read it
from a stream or whatever.

regards

Frank
On Apr 4, 3:54 pm, Frank Werner <frank.werner-krippend...@franke.com>
wrote:
>then this:http://www.codeproject.com/cs/algori...heval.aspwould
fit your needs

Regards

Frank
>>On Apr 4, 3:23 pm, Frank Werner
<frank.werner-krippend...@franke.comwrote:

Hello Roderick,

generally yes,

see:http://www.codeproject.com/useritems...de_Generation1.
as p

Regards

Frank

Is it possible to create dynamic code which is not part of a class
?- Hide quoted text -
>
- Show quoted text -

but that creates an assembly....right ? is it possible to run it
directly ?- Hide quoted text -
- Show quoted text -
and is it possible to compile the code without putting it in a
class...example code = "Console.WriteLine("Hello");"

Apr 4 '07 #7
Ro***************@gmail.com wrote:
Is it possible to create dynamic code which is not part of a class ?
Yes, via DynamicMethod.

-- Barry

--
http://barrkel.blogspot.com/
Apr 4 '07 #8
The string variable 'str' contains code that will be compiled on the fly
and executed. For a simpler example,
let me know if this is what you meant by dynamic code.

You will need to compile the "base.dll" separately and modify the path
as well. Base.dll could be a simple class with a constructor.

using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Windows.Forms;
using System;
using System.Reflection;
public class t
{
static void Main(){
Base myobj;
CSharpCodeProvider csp= new CSharpCodeProvider();
ICodeCompiler ic = csp.CreateCompiler();
CompilerParameters cparam= new CompilerParameters();
cparam.GenerateInMemory = true;
cparam.GenerateExecutable = false;
cparam.ReferencedAssemblies.Add("system.dll");
cparam.ReferencedAssemblies.Add(@"c:\c#_prgs\base. dll");
string str = "using System;"+
"class myclass" +
"{"+
"public myclass(){i=123;}"+
"public int i;"+
"public string ToString()"+
"{"+
"return i.ToString();"+
"}" +
"}";
CompilerResults cres= ic.CompileAssemblyFromSource(cparam,str);
foreach (CompilerError ce in cres.Errors)
MessageBox.Show(ce.ErrorText);

if (cres.Errors.Count == 0 && cres.CompiledAssembly != null)
{
Type ObjType = cres.CompiledAssembly.GetType("myclass");
try
{
if (ObjType != null)
{
myobj = (Base)Activator.CreateInstance(ObjType);
Console.WriteLine(myobj.ToString());
FieldInfo[] f= ObjType.GetFields();
foreach(FieldInfo ff in f)
{
MessageBox.Show(ff.Name + "=" + ff.GetValue(myobj).ToString());
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}

with regards,
J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com

*** Sent via Developersdex http://www.developersdex.com ***
Apr 9 '07 #9
Thanks a lot, but i was checking if it is possible to say for
example...
string mycode = "Console.WriteLine("Hello");";
then compile and run the code in mycode without putting in a class
name etc.. thanks.

Apr 10 '07 #10
On Apr 10, 9:12 am, Roderick.Buhag...@gmail.com wrote:
Thanks a lot, but i was checking if it is possible to say for
example...
string mycode = "Console.WriteLine("Hello");";
then compile and run the code in mycode without putting in a class
name etc.. thanks.
Well, you could always have an autogenerated (or even fixed, dummy)
class name, and the rest of the class structure. It's a bit ugly,
given that it'll have to generate a whole assembly etc, but I don't
know of any other way of doing it.

Jon

Apr 10 '07 #11
On Apr 10, 10:15 am, "Jon Skeet [C# MVP]" <s...@pobox.comwrote:
On Apr 10, 9:12 am, Roderick.Buhag...@gmail.com wrote:
Thanks a lot, but i was checking if it is possible to say for
example...
string mycode = "Console.WriteLine("Hello");";
then compile and run the code in mycode without putting in a class
name etc.. thanks.

Well, you could always have an autogenerated (or even fixed, dummy)
class name, and the rest of the class structure. It's a bit ugly,
given that it'll have to generate a whole assembly etc, but I don't
know of any other way of doing it.

Jon
exactely that was my problem...thats why I asked maybe there's a way
to eliminate having to generate a whole assembly etc....

thanks

Apr 10 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Guinness Mann | last post by:
When you guys talk about "dynamic SQL," to what exactly are you referring? Is dynamic SQL anything that isn't a stored procedure? Specifically, I use ASP.NET to communicate with my SQL Server...
6
by: chris | last post by:
Hi all, I need to know, what is the difference between dynamic memory allocation, and stack allocation ? 1. If I have a class named DestinationAddress, when should I use dynamic memory...
6
by: Materialised | last post by:
Hi Everyone, I apologise if this is covered in the FAQ, I did look, but nothing actually stood out to me as being relative to my subject. I want to create a 2 dimensional array, a 'array of...
3
by: Stephen Gennard | last post by:
Hello, I having a problem dynamically invoking a static method that takes a reference to a SByte*. If I do it directly it works just fine. Anyone any ideas why? I have include a example...
7
by: Abraham Luna | last post by:
how do i stop the dynamic validators from breaking explorer if i use a dynamic validator and move to a different control it breaks explorer and i can type in the page when i'm not supposed to....
0
by: Pascal Costanza | last post by:
Dynamic Languages Day @ Vrije Universiteit Brussel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Monday, February 13, 2006, VUB Campus Etterbeek The VUB (Programming Technology Lab,...
7
by: Mike Livenspargar | last post by:
We have an application converted from v1.1 Framework to v2.0. The executable references a class library which in turn has a web reference. The web reference 'URL Behavior' is set to dynamic. We...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
5
by: bearophileHUGS | last post by:
I often use Python to write small programs, in the range of 50-500 lines of code. For example to process some bioinformatics data, perform some data munging, to apply a randomized optimization...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.