views:

127

answers:

4

I made a program that achieve a knowledge representation, its good but I thing it could be better if I can create and destroy classes on the fly.

+1  A: 

If you are talking about dynamic code generation, here is a link:

dynamic code in c# dynamic code in script

kniemczak
+1  A: 

In C# 3.0 it's possible to create anonymous types, though as STW points out, this is not defined at runtime, but compile time.

var myClass = new { A = 5, Id = "MyClass", num = 0.187f};

Console.WriteLine(myClass.Id);

If you're looking to actually create a new class type on the fly, a round-about way is to generate C# class files, compile them into an assembly, and use Reflection to load the classes.

Alan
Anonymous types are neither created, nor "deleted" at runtime. They are standard classes with compiler-generated names and some special permissions/rules--but they are in no way dynamic.
STW
@STW: yes, an important distinction. I had taken the meaning of "dynamic" to mean without explicitly defining a class. However you are quite right.
Alan
anonymous types are part of c# 3.0, which is part of .net 3.5 not .net 3.
Ben Robinson
got an error on this:var myClass = new { int A = 5, string Id = "MyClass", float num = 0.187f}; ...Error 7 An anonymous type cannot have multiple properties with the same name.Framework 3.5
Delta
@Ben: C# 3 works perfectly well on .net 2.0 as well.
erikkallen
@erikkallen Technically you are talking about the CLR 2.0 not .net 2.0. Without the assemblies that come with .net 3.5 you cannot use c# 3
Ben Robinson
@user327104: yeah, sorry, omit the property types.
Alan
+1  A: 

From your description, it sounds like you don't really want to create C# classes at runtime. What you want is some sort of representation of knowledge.

There is no simple answer to this. This is an area of artificial intelligence research. See http://en.wikipedia.org/wiki/Knowledge_representation_and_reasoning for an introduction.

Kristopher Johnson
+1  A: 

To me it sounds like you want to learn some AI programming and knowledge representation. However this is far different from creating classes at runtime. Wikipedia gives a rather good introduction into the subject, but no code at all.

http://en.wikipedia.org/wiki/Artificial_intelligence

Eloff