dynamic types

  • Thread starter Thread starter Stephen Lamb
  • Start date Start date
S

Stephen Lamb

How would one do the following at runtime? I'm really interested in steps 2
and 3.

1. Read data from somewhere that describes types and instances.
2. Construct new types from data.
3. Construct instances of types from data.
3. Use new types and instances in a type safe manner.

e.g.

1. Read data from somewhere that describes types and instances.
Create a type that is a struct called Foo with members E1 and E2 that are 32
bit ints.
Construct a Foo with E1 = 23 and E2 = 5.

2. Construct new types from data.
public struct Foo
{
public Int32 E1;
public Int32 E2;
}

3. Construct instances of types from data.
Foo foo = new Foo();
foo.E1 = 23;
foo.E2 = 5;

4. Use new types and instances in a type safe manner.
foo.E1 = 23;


Thanks,
Steve
 
Hi Stephen,
How would one do the following at runtime? I'm really interested in steps 2
and 3.

1. Read data from somewhere that describes types and instances.
2. Construct new types from data.
3. Construct instances of types from data.
3. Use new types and instances in a type safe manner.

See the System.Reflection.Emit namespace.

bye
Rob
 
Hi Stephen,

1. Read data from somewhere that describes types and instances.
Create a type that is a struct called Foo with members E1 and E2 that are
32
bit ints.
Construct a Foo with E1 = 23 and E2 = 5.

This is possible and not hard. Look at System.CodeDom namespace types.
For samples how to generate and compile types at run time refer to the
followinf MSDN article

[offline]
ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpcongeneratingsourcecodecompilingprogramfromcodedomgraph.htm
[online]
http://msdn.microsoft.com/library/d...ourceCodeCompilingProgramFromCodeDOMGraph.asp
2. Construct new types from data.
public struct Foo
{
public Int32 E1;
public Int32 E2;
}

This is possible and not hard. Look at System.CodeDom namespace types.
For samples how to generate and compile types at run time refer to the
followinf MSDN article

[offline]
ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpcongeneratingsourcecodecompilingprogramfromcodedomgraph.htm
[online]
http://msdn.microsoft.com/library/d...ourceCodeCompilingProgramFromCodeDOMGraph.asp
3. Construct instances of types from data.
Foo foo = new Foo();
foo.E1 = 23;
foo.E2 = 5;

4. Use new types and instances in a type safe manner.
foo.E1 = 23;

AFAIK 3 and 4 are not possible in the way you want to used. The Compiler
won't compile those code snippets. The reason being is that the compiler
doesn't know anything about that Foo type. at compile time it doesn't exist
The only way to use that type is to use reflection and discover type info at
run time.
The other possibility is to use well-known interface, but this is not what
you are after. Using interface means that there is no strong typing
 
You could also use the CodeDom to emit the code, load compile the assembly and load it. To use Reflection.Emit you have to output the IL opcodes (to add method bodies), to use the codedom you emit in language neutral contrstructs (to add method bodies). However, you need to go through a compilation process to create the assembly

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

Hi Stephen,
How would one do the following at runtime? I'm really interested in steps 2
and 3.

1. Read data from somewhere that describes types and instances.
2. Construct new types from data.
3. Construct instances of types from data.
3. Use new types and instances in a type safe manner.

See the System.Reflection.Emit namespace.

bye
Rob
 
Stephen Lamb said:
How would one do the following at runtime? I'm really interested in steps 2
and 3.

1. Read data from somewhere that describes types and instances.
2. Construct new types from data.
3. Construct instances of types from data.
3. Use new types and instances in a type safe manner.

e.g.

1. Read data from somewhere that describes types and instances.
Create a type that is a struct called Foo with members E1 and E2 that are 32
bit ints.
Construct a Foo with E1 = 23 and E2 = 5.

2. Construct new types from data.
public struct Foo
{
public Int32 E1;
public Int32 E2;
}

3. Construct instances of types from data.
Foo foo = new Foo();
foo.E1 = 23;
foo.E2 = 5;

4. Use new types and instances in a type safe manner.
foo.E1 = 23;

Thanks to all for the pointers. I had only taken a very quick look at
CodeDom so any pointers on how to use it is helpful.

As Stoitcho Goutsev pointed out, my idea isn't really possible in C# since
this is a compiled language. I'm guessing it might be possible in a really
dynamic system like Smalltalk.

I guess my next best option would be to automatically generate the types
ahead of time using CodeDom and then compile them into the assembly that
will read the construction data and instantiate instances. The code that
does the reading of type data can compare the type compiled into the
assembly to the type specified by the read data. I'll probably just use
versioning to compare the two type descriptions and then only do the real
check in debug.

Thanks,
Steve
 
Back
Top