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
-
1How do you expect to have intellisence on classes that are created at runtime?Roman– Roman2010年05月14日 12:29:07 +00:00Commented May 14, 2010 at 12:29
-
@RomanArmy - question updated - hopefully this will make more sense!JL.– JL.2010年05月14日 12:33:40 +00:00Commented 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?Roman– Roman2010年05月14日 12:49:54 +00:00Commented 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.JL.– JL.2010年05月15日 20:52:29 +00:00Commented 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...Peter Gfader– Peter Gfader2010年05月16日 01:35:51 +00:00Commented May 16, 2010 at 1:35
4 Answers 4
Use this sample implementation of a PropertyBag.
If property doesn't exist, create it on the fly...
-
This isn't what I wanted, but its the best the current technology use has to offer, thanks!JL.– JL.2010年05月15日 20:53:12 +00:00Commented 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!Peter Gfader– Peter Gfader2010年05月16日 01:34:59 +00:00Commented May 16, 2010 at 1:34
If you want emit code at runtime?
Checkout the Reflection.Emit namespace
OR better
RunSharp - nicer API
-
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=4Dan Bryant– Dan Bryant2010年05月14日 13:35:02 +00:00Commented May 14, 2010 at 13:35
What you want is XML, based on a schema. This will give you IntelliSense, including code snippets, at the same time as providing flexibility.
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?
-
"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.Eight-Bit Guru– Eight-Bit Guru2010年05月14日 14:07:49 +00:00Commented May 14, 2010 at 14:07