473,396 Members | 2,036 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,396 software developers and data experts.

GetNestedType returning null...

Hi There! :O)

I've been trying this sample over here :
http://groups.google.com/groups?thre...%40tkmsftngp11

Unfortunately the GetNestedType() function returns null no matter what enum
member i'm passing to it. Does any one has an idea why? We kind of need to
build an enum one the fly and this sample seemed to be the way to go.

TIA!

--
Best Regards
Yanick Lefebvre
Nov 16 '05 #1
3 3498
Hi Zoury,

Based on my understanding, you want to dynamically create a Enum and set
its value correctly.

Yes, the article way of using Reflection.Emit is correct, but when setting
its value, I think we should not use Type.GetNestedType method.

Type.GetNestedType method searches for the nested type with the specified
name. But for the Enum, the "pawn" is not a nested type, it is just a
nested field. So Type.GetNestedType method will always return null.

FieldInfo.SetValue method's second parameter is "The value to assign to the
field". Because it is a Enum type, you should specify Enum object for it,
you should not specify a type object for it.

So, you should do like this:

AppDomain ad = AppDomain.CurrentDomain;
AssemblyName an = new AssemblyName();
an.Name = "MyDynamicAssembly";
AssemblyBuilder ab = ad.DefineDynamicAssembly(an,
AssemblyBuilderAccess.Run);
ModuleBuilder mb = ab.DefineDynamicModule("MyDynamicModule");
TypeBuilder tb = mb.DefineType("MyDynamicClass");
EnumBuilder eb = mb.DefineEnum("enumChess",
TypeAttributes.Public,typeof(int));
eb.DefineLiteral("pawn", 0);
eb.DefineLiteral("castle", 1);
eb.DefineLiteral("king", 2);
Type tEnum = eb.CreateType();

if (tEnum != null)
{
FieldBuilder fb = tb.DefineField("theChess", tEnum,
FieldAttributes.Public);
Type tClass = tb.CreateType();
if (tClass != null)
{
object o = Activator.CreateInstance(tClass);
FieldInfo fi = tClass.GetField("theChess");

object enumvalue=Enum.Parse(tEnum, "king");

fi.SetValue(o, enumvalue);
string s = fi.GetValue(o).ToString();
MessageBox.Show(s);
}
}

It works well on my machine.

========================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 16 '05 #2
Hi Jeffrey! :O)
object enumvalue=Enum.Parse(tEnum, "king");


Neat! thanks a lot for your input!

--
Best Regards
Yanick Lefebvre
Nov 16 '05 #3
Hi Zoury,

I am glad I can help you :-) You are welcome!!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Pablo J Royo | last post by:
Hello: i have a function that reads a file as an argument and returns a reference to an object that contains some information obtained from the file: FData &ReadFile(string FilePath); But ,...
4
by: Ellen Manning | last post by:
Using SQL2000. I want to return the # of columns with non-null values. Here's my query so far: select case when Dx1 is not null then 0 else 1 end + case when Dx2 is not null then 0 else 1 end...
11
by: kelvSYC | last post by:
What is the best way to create functions that, based on some input, return either structures or a null value if the structure cannot be made? The problem is that the structure has be created...
19
by: JKop | last post by:
When I compile and run the following on my system: #include <iostream> static int hello = 78; int ReturnValue(void) {
5
by: Gent | last post by:
I have two questions which are very similar: Is it possible to return an object in C++. Below is part of my code for reference however I am more concerned about the concept. It seems like the...
11
by: JKop | last post by:
AnyClass Blah() { AnyClass poo; return poo; } As we all know, in the above, the compiler is entitled to:
19
by: Rick | last post by:
Hi, I was wondering, can it be safely assumed that any function that returns a *char will return a NULL terminated string? Or does the function explicitly mention this always (and the ones that...
1
by: john | last post by:
Relatively new to C coding, so any help would greatly be appreciated. I'm having problems try to return my string array from my parsing function. When I do a printf I am getting the correct value...
9
by: Thomas Mlynarczyk | last post by:
Hi, It seems to be a generally adopted convention to have a function return FALSE in case of an error. But if a function is supposed to return a boolean anyway, one cannot distinguish anymore...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.