473,394 Members | 1,761 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

c# question in Reflection(please help)

I'm using reflections to dynamically build an enum type from xml values. the xml looks like..

Expand|Select|Wrap|Line Numbers
  1. <Students>
  2.         <Student>
  3.             <Name>John</Name>
  4.             </Student>
  5.         <Student>
  6.             <Name>Smith</Name>
  7.          </Student>
  8.  </Students>
-----
I want to declare a property from the reflected type. in other words I want to have the "Profile provider" behaviour when aquiring profile properties, a list of enumerated values appears as I add them to the web.config

I want to have my enum list like this

Students.John
----------.Smith

------------------------------- <CODE> -----------------------------------
Expand|Select|Wrap|Line Numbers
  1. public static Type StudentTypes
  2.     {
  3.              get 
  4.              {
  5.                  AppDomain currentDomain = AppDomain.CurrentDomain;
  6.                  AssemblyName aName = new AssemblyName("TempAssembly");
  7.                  AssemblyBuilder ab = currentDomain.DefineDynamicAssembly(aName, AssemblyBuilderAccess.RunAndSave);
  8.                  ModuleBuilder mb = ab.DefineDynamicModule(aName.Name, aName.Name + ".dll");
  9.                  EnumBuilder eb = mb.DefineEnum("StdType", TypeAttributes.Public, typeof(int));
  10.  
  11.  
  12.                  XmlDocument xml = new XmlDocument();
  13.                  xml.Load(HttpContext.Current.Server.MapPath("~") + ConfigurationManager.AppSettings["AdvertisesConfiguration"]);
  14.  
  15.                  XmlNodeList xnList = xml.SelectNodes("/Students/Student");
  16.                  int i = 0;
  17.                  foreach (XmlNode xn in xnList)
  18.                  {
  19.                      eb.DefineLiteral(xn["Name"].InnerText, i++);
  20.                  }
  21.                  Type finished = eb.CreateType();
  22.                  return finished; 
  23.              }
  24.          }
------------------------------- </CODE> -----------------------------------


------- thank you
Nov 29 '08 #1
5 1502
now I have dynamically constructed an enum type. is there any way I can declare a variable/property using this type. so I can expose its inner members to the declared variable/property??
Nov 30 '08 #2
Plater
7,872 Expert 4TB
@lamoureternal
Not in the same way as if the enum had been coded in at design time. You will have to use reflection for everything
Dec 1 '08 #3
balabaster
797 Expert 512MB
@Plater
Is there a way of having reflection build the enum at design time, in a similar fashion to the way that member attributes such as [Extension()] tell the Visual Studio compiler to build an enum?

It seems to me that inside Visual Studio this is done somewhere... all that would be happening is that the enum is defined in an XML doc rather than in a CS/VB document...

That said, I have no clue how I would go about this, I would have to have a play to figure it out. I've come across need to do this in the past where large enums are far easier to manage and synchronize from the database than they are from the application, i.e. you don't want to be reporting on magic numbers and exporting an enum to the database every time you change it can be a pain too. It's far easier if the enum is built on the fly from the database and all references in the application are updated on the fly.

How you would get around that without a recompile is beyond me though...seems like it might be more of a headache in reality than it is in theory...
Dec 1 '08 #4
I couldn't agree more balabaster, that's exactely what drived me at the first place to go that way.

I know it's possible. actually the Profile provider's using it, when you define properties through the web.config they appears as static members of the Profile collection (at design time).

so. it's close :)
Dec 2 '08 #5
Plater
7,872 Expert 4TB
When you define values in the web.config, an implicit re-compile occurs, creating the class structures.
What if you had roughly a Dictionary<string, int> where the string portion is the key and contains your enum names and the int portion contains your values.
You can create that object from reflection
Dec 2 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
0
by: A. Wiebenga | last post by:
Hi all! I am a student at the Hogeschool van Arnhem en Nijmegen in Holland. I am currently involved in a research project regarding Reflection. Purpose of the research project is to document...
10
by: Sunny | last post by:
Hi, I have an old problem which I couldn't solve so far. Now I have found a post in that group that gave me an idea, but I can not fully understand it. The problem is: I'm trying to use a...
2
by: Alexandre | last post by:
Hi, im currently working on making myself a MySql Object Abstraction Layer so that i pass in objects and it does the database trasnsactions using reflection so far everything works except the...
1
by: Mudassar | last post by:
i want to get the property value using reflection. Scenerio: i have a status bar on MDI form. it has property named "Panels" and i want to get a specific panel from that panels collection using...
6
by: Andrew Robinson | last post by:
assuming that I have a class public class MyClass { public string FirstName { get {...} set {...} } } how can I assign the FirstName property using reflection on a generic class
3
by: Rain | last post by:
Hi, i need help please.. any help would be greatly appreciated. I need to know how i can get the value of a property throught reflection? please please.. thanks in advance...
4
by: =?Utf-8?B?QWJoaQ==?= | last post by:
I am using Reflection to invoke methods dynamically. I have got a special requirement where I need to pass a value to method by setting the custom method attribute. As I cannot change the...
7
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
I have a C# logging assembly with a static constructor and methods that is called from another C# Assembly that is used as a COM interface for a VB6 Application. Ideally I need to build a file...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.