Click here to Skip to main content
13,004,188 members (73,248 online)
Click here to Skip to main content
Add your own
alternative version


8 bookmarked
Posted 31 May 2008

Generating types from external metadata

, 31 May 2008
Rate this:
Please Sign up or sign in to vote.
Type generation from external metadata.


This is a solution for creating types that are defined in an external metadata. The idea is to define types that can be used in the application without compiling or changing the code.


Along with types that are defined (like in my example, in an external XML file), data bindings and data access to database can be stored in external meta data. So by having such meta data and using Designer hosting, a very usefully customization solution can be provided. Advanced users of the application can design and use their own, for example, WinForms without writing a single line of code.

Using the Code

There are several classes involved. The main one is TypeMetaData, which describes the type. The properties AssemblyName, ModuleName, and TypeName are mandatory since they are used in the process of type creation. All the properties are stored in the List Properties, whose items are of type PropertyMetaData. PropertyMetaData describes the property type. Since this is just a basic example, we have just the property name and property type. In addition, we can add property attributes or any other related metatdata. The class TypeXmlSerializer is used to serialize and deserialize the type metadata to XML. Type generation happens in the class TypeGenerator. There is a method GenerateType(TypeMetaData metaData) whose responsibility is to generate the type from the given meta data.

Below is an example of using the TypeGenerator:

// Generating type from xml

OpenFileDialog dlg = new OpenFileDialog();
dlg.DefaultExt = "xml";
dlg.Filter = "XML Files|*.xml";

if (dlg.ShowDialog() == DialogResult.OK)

   TypeMetaData meta = TypeXmlSerializer.DeSerialize(dlg.FileName);
   object newType = TypeGenerator.GenerateType(meta);

TypeMetaData should be deserialized from the external source and then used to generate the type.


I hope that this short article was useful and that it can give ideas to anyone who needs to design and implement dynamic type creation.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Bojan Petkovic
Software Developer Pilat Media
Israel Israel
No Biography provided

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170627.1 | Last Updated 31 May 2008
Article Copyright 2008 by Bojan Petkovic
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid