Is it possible to do the "right-click, add, new-item" process in Visual Studio 2012's solution explorer, exclusively through code?

StackOverflow https://stackoverflow.com/questions/17535312

Question

I created a wizard that displays a form generated solely with code. This form lets the user navigate some databases and data sets, ending with the selection of some data. Once they've done that and some variables have assigned/changed, the form should just present them with an "ok" button. So far, I have all of this done.

My problem is, once that button is clicked I'd like to add a custom class object/entity that I created to the current solution. I have no idea (and cannot find anything that helps) how to add the new custom class, namespaces and all, exclusively through code.

If any one has any pointers or experience with this it'd be greatly appreciated!

Was it helpful?

Solution

This technic called metaprogramming. There is nice book named "Metaprogramming in .NET" that shows you how to use cool features.

So, for metaprogramming purposes .NET offers:

  1. Reflection - used to find out info about classes, to get access to private/protected/internal field and properties and etc. And one another overview on codeproject: link
  2. Reflection.Emit - gives ability to create new types at runtime.
  3. Expression trees - for storage code as data. It allows to you generate methods in ugly syntax (which is not so ugly as IL-codes), compile them and get IL code from compiled lambda-function.
  4. CodeDom - provides templated code generation and dynamic compilation.
  5. Text Template Transformation Toolkit aka T4 - beforecompile-time metaprogramming.
  6. Binary Weaving - aftercompile-time metaprogramming. Smth like Fody and PostSharp. Not your case, just for overview
  7. And special feature: .NET have part named "Dynamic Language Runtime" that provides features of dynamic-typed languages to .NET. There is ExpandoObject class that gives you ability to define undefined properties

So, that was short overview of metaprogramming features of .NET. Now, about your case. If you want to add new class right to code of your solution, really in code in filesystem, then you free what to choose, you even can write code just as text.

But if you want to add new class to runtime application, firstly you must decide why you need to add new class, how you will use it?

So, main idea: firstly you describe class, then add it to runtime. Now you have ugly syntax to create object of this class. It can be Activator.CreateInstance(yourRuntimeTypeInfo) or yourRuntimeTypeInfo.GetConstructor(...).Invoke(...). What next? All what you get from this create methods at compile time is just an Object. You must use reflection, find method, call it: yourRuntimeTypeInfo.GetMethod(...).Invoke(...). Or you could generate method info with expression trees, determine basic class for all your database classes; and use generated method in basic class in someway. OR there is a way. Don't add new types, just use ExpandoObject (or ElasticObject), and describe his methods as lambdas; after that all just call methods. Or you even can just use Dictionary. Or you can think about it all a bit more and find solution without metaprogramming.

OTHER TIPS

Try using Dynamic compiling and another article.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top