|
Hi
I am struggling over getting the enum items name using reflection
say this the enum
/ Enum for Art Mode <br />
public enum tenumArtMode<br />
{<br />
artModeAuto=0,<br />
artModeManual,<br />
artModeInvalid<br />
} ;
Note:: this enum is in dll which is developed in VC++ .NET, I have added reference to access this enum in C#
I want to display this enum items using C#
I have tried the function Enum.GetName
tenumArtMode tenumMode;<br />
Type t=tenumMode.GetType();<br />
<br />
Enum.GetName(t.FieldType,t.GetValue(tenumMode));
But this throws me an ArgumentException
TIA
Srini
|
|
|
|
|
You can also simply do a ToString(). For example:
string s = tenumArtMode.arModeManual.ToString();
Would yield the string "artModeManual"
Good Luck
Mike Luster
CTI/IVR/Telephony SME
|
|
|
|
|
Thanks For your reply Mike,
No, This can't be applicable, b'cas i getting this enum at runtime so i would not know the enum item names.
Do understand my question. I want to iterate the enum using reflections
Regards,
Srini
|
|
|
|
|
You don't need reflection. The Enum class provides you with methods to get all information you nead about the enum.
Use the GetNames method to get an array of all the names. If you need their values, use the GetValues method to get an array with all the values.
---
b { font-weight: normal; }
|
|
|
|
|
If it is One Enum then your suggestions is OK. I can use Enum.GetNames(enum), But My situation is likes a structure which will contain lot of enums(say 100) as it member) so i can't write 100's lines of code to display every member
ie.
enum1<br />
{<br />
a=0,<br />
b,<br />
c<br />
}<br />
..<br />
enum100<br />
{<br />
x=0,<br />
y,<br />
z<br />
}<br />
<br />
Struct st<br />
{<br />
enum1 e1;<br />
enum2 e2;<br />
..<br />
enum100 e100;<br />
};<br />
<br />
Enum.GetNames(st.e1);<br />
..<br />
Enum.GetNames(st.e100);
Instead Puting like the above
I am thinking of reflection to reduce the line of code
Like this
Type ty = st.GetType();<br />
FieldInfo[] fin= ty.GetFields();<br />
foreach (FieldInfo fino in fin)<br />
{<br />
fino.GetValue(st).ToString();<br />
}
But This print the enum1..enum100 values like 0 0 0 0 ..
but I want to print the enum member names like a b c ...
Hope I Explained my question Clearyly
Regards,
Srini
|
|
|
|
|
I see. Try this, then:
Enum.GetName(fino.FieldType, fino.GetValue(st))
---
b { font-weight: normal; }
|
|
|
|
|
Exactly thats what i want, i have used the same code but it throws me me ArgumentException, i think since the enum is developed in VC++ .net fino.FieldType doesn't match with System.Enum Type in C#(since we are using Enum.GetName).. I think so.
Try it out and if anything works please share with me.
Regards,
Srini
|
|
|
|
|
Have you tried it out? It's not the same code as you showed in your first post.
---
b { font-weight: normal; }
|
|
|
|
|
Yes I tried out. it throws me the ArgumentException.
ArgumentException - Description given in the MSDN help
enumType is not an Enum.
-or-
value is neither of type enumType nor does it have the same underlying type as enumType.
Enum in VC++ .net and VC# .net are they different?
|
|
|
|
|
Even That is code i have mentioned in my initial Conversation.
|
|
|
|
|
No, it's not. There you used a Type object instead of a FieldInfo object.
---
b { font-weight: normal; }
|
|
|
|
|
Have you tried using the Enum struct? Once you have the type of enum you can use the static methods of the Enum struct to obtain information about the enum type.
chriswa
|
|
|
|
|
I got it!!!!
Thanks Chriswa its working as per your suggestion. i have declared the enum as enum class (ie enum struct) in VC++ dll. then i can able to get the enum member names.
If i declare the enum using enum in VC++, then i am not able to retrieve the names.
What is the difference between enum and enum struct
I extend my thanks to Guffa.
Regards,
Srini
|
|
|
|
|
How does one get the current date and time in C#. Is there any readymade class.
|
|
|
|
|
deepscyberpulse wrote: How does one get the current date and time in C#. Is there any readymade class.
DateTime.Now
Share and enjoy.
Sean
|
|
|
|
|
I think that this is what u want.
DateTime d = new DateTime();
MessageBox.Show(d.ToShortDateString());
|
|
|
|
|
Hi, you can use this:
label1.Text = DateTime.Now.ToLongDateString(); //date
label2.Text = DateTime.Now.ToLongTimeString(); //time
Regards,
Alberto Martinez
|
|
|
|
|
Hello
I am working on an application in c# i want to develop a sofware that block the any address that is contain in the database of my appliction. i.e if i have www.yahoo.com in my database and user enter www.yahoo.com in the address bar of the Internet Explorer this site should not be open.
If any one has worked on it or have idea about it plz le me know.
|
|
|
|
|
This sounds like a task better suited for native code. To block addresses on the client machine, you're going to have to write some native hooks into the OS to truely prevent this. If you're just blocking Internet Explorer navigation, you could write hooks or a plugin for IE that blocks the site. Either way, definitely not a task for C# and .NET.
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Moral Muscle
The apostle Paul, modernly speaking: Epistles of Paul
Judah Himango
|
|
|
|
|
Judah Himango wrote: definitely not a task for C# and .NET.
Why not?
I agree though that native code is better, but you can accomplish the same thing in .NET
Ed
|
|
|
|
|
I would agree. In fact this sounds like something that you would want to do in ASP.NET of you are blocking the address from the server. It not actually clear in the grandparent that this has to be done on the client. If this has to be done on the client then you can build a plug in from .NET using COM/Interop or build an IE plug-in from COM. It is easier at times to use .NET to write a COM plug-in
chriswa
|
|
|
|
|
ASP.NET? It'd be possible but I'd have thought I'd be better to have a service running on the server / client which interops with the Win32 API.
You notice that's how most firewalls do it
The only major difference would be that it would be written in .NET with COM Interop rather than in native code.
Ed
|
|
|
|
|
Hi all
i have displayed a CSV file (coma separated) in a richtextbox,
but now need to put it into an Ilist
Anyone got any ideas ??
Thanks lots
John
|
|
|
|
|
Search this site for CSV parsers. There are many out there freely available.
|
|
|
|
|
What would be the equivalent in C# for the following?
Sub CreateADODB()<br />
Dim adoApp As Object<br />
adoApp = CreateObject("ADODB.Connection")<br />
End Sub<br />
Is there a site where you can enter functions of VB and see what the C# (and/or other languages) are?
thanks!
No hurries, no worries.
|
|
|
|