3

Is it possible to create objects at designtime without having to have hard coded class definitions, then populate properties with primitives or even strongly typed data types?

This might sound confusing, so I will attempt to give you a use case scenario.

Use case:

You have an XML config file that could hold configuration values for connecting to various systems in an SOA application. In C# the XML file is read, but for each system the configuration properties are different (e.g: SQL might have a connection string, while SharePoint might need a username + password + domain + url, while yet an smtp server would need username + password + port + url)

So instead of creating static classes as follows

public class SharePointConfiguration or public class SQLConfiguration, then have each class with custom properties (this is cumbersome)

or

using a 1990's method, an ArrayList or some named collection

Is there not a more preferred way to achieve this? Taking advantage of new language features, that can still offer design time intellisense, which would make the code easier to maintain and less prone to error.

I guess I am looking for some kind of multipurpose .net 4 property holder. Thanks

Irshad
3,1315 gold badges33 silver badges57 bronze badges
asked May 14, 2010 at 12:22
5
  • 1
    How do you expect to have intellisence on classes that are created at runtime? Commented May 14, 2010 at 12:29
  • @RomanArmy - question updated - hopefully this will make more sense! Commented May 14, 2010 at 12:33
  • You're gonna have to pick between runtime object generation and intellisence. I don't think those two requirements can ever be satisfied (at the same time). Perhaps a regular code generation solution (like T4) would suit your needs better? Commented May 14, 2010 at 12:49
  • ok I guess what I am after is something up and coming in some future release of visual studio (not 2010), I really want intellisense extended to be able to read paired key names. Commented May 15, 2010 at 20:52
  • If you are dealing just with XML files, generate a class for those with "xsd"... a bit tricky to do this at runtime though... Commented May 16, 2010 at 1:35

4 Answers 4

1

Use this sample implementation of a PropertyBag.
If property doesn't exist, create it on the fly...

http://www.codeproject.com/KB/recipes/propertybag.aspx

answered May 14, 2010 at 13:02
2
  • This isn't what I wanted, but its the best the current technology use has to offer, thanks! Commented May 15, 2010 at 20:53
  • Are you after constructing something more complex? This is an interesting problem... Let us know more what you are after! Commented May 16, 2010 at 1:34
1

If you want emit code at runtime?

Checkout the Reflection.Emit namespace
OR better
RunSharp - nicer API

answered May 14, 2010 at 12:32
1
  • I have one application where we dynamically generate a class structure, including XML documentation to support Intellisense, using a sort of 'compiler' for a specialized data structure. It's quite a lot to bite off though (the IL Emit API doesn't do a whole lot to protect you from doing stupid things) and is overkill for creating configuration classes. Look into LINQ to XML: msdn.microsoft.com/en-us/library/bb387098.aspx?ppud=4 Commented May 14, 2010 at 13:35
1

What you want is XML, based on a schema. This will give you IntelliSense, including code snippets, at the same time as providing flexibility.

answered May 14, 2010 at 13:05
0

Based on your question (and assuming I'm reading it right), that would be impossible. The closest you could get would be to use the 'dynamic' type and assign your values to properties at runtime on it - the problem being, that dynamic has no Intellisense support, and even with some other kind of solution, the Intellisense would not be available because the properties would only be attached at runtime.

Am I confused on what you are asking?

answered May 14, 2010 at 12:25
1
  • "Dynamic" should not be used in this way, it's essentially short-circuiting the strong typing of the language. Its preferred use is in the context of COM and other interop scenarios where objects are instantiated at runtime. Commented May 14, 2010 at 14:07

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.