Reflection of Enum and cannot get TypeOf original Type

S

sippyuconn

Hi

Using Reflection to get an Enumeration value that I pass to another DLL
The dll has a check of incoming Enum paramater is of specific type

The checl always fails because it thinks the Enum if of Type FieldInfo
Is there a way to cast to Original type ???

Thanks

//Reflection to get Enum and pass to Dll Method

Type enumType = GenericDLL.GetType("MyTest.InputFields");
System.Reflection.FieldInfo enumItem = enumType.GetField("Enum1");

Method1(enumItem );

//Dll method

Method1(object o1)
{
//This method always fails - gets type FieldInfo
if (o1 .GetType() == typeof(InputFields))
MessageBox.Show("OK");

}
 
S

sippyuconn

//Test EXE Calls DLL


//Setup Assembly we need thru Reflection
string sPath =
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) +
"\\Worker.dll";
Assembly WorkerDLL = Assembly.LoadFile(sPath);
// Get the type of the needed object:
Type Engine = WorkerDLL.GetType("MyNameSpace.Worker");
// Create an instance of the needed object:
object myEngine = Activator.CreateInstance(Engine);


MethodInfo putdata = Engine.GetMethod("PutData");

int rc = 0;
Type enumType = WorkerDLL.GetType("MyNameSpace.InputFields");
System.Reflection.FieldInfo enumItem = enumType.GetField("Version");
int x = (int)enumItem.GetValue(enumType);
// package our parameters
object[] arrParms = new object[3];
arrParms.SetValue(enumItem, 0);
arrParms.SetValue(i, 1);
arrParms.SetValue(sData, 2);
rc = (int)getdata.Invoke(myEngine, arrParms);





//My DLL


namespace MyNameSpace
{

public enum InputFields
{
Family,
Version,
Width,
Size,
}




public class Worker
{

public Worker()
{}

public int PutData(object field, int occurrence, string data)
{
int rc = 0;

//CAnnot get match here
if (field.GetType() == typeof(InputFields))
{

}

return(rc);
}

}
}
 
S

sippyuconn

//Test EXE Calls DLL


//Setup Assembly we need thru Reflection
string sPath =
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) +
"\\Worker.dll";
Assembly WorkerDLL = Assembly.LoadFile(sPath);
// Get the type of the needed object:
Type Engine = WorkerDLL.GetType("MyNameSpace.Worker");
// Create an instance of the needed object:
object myEngine = Activator.CreateInstance(Engine);


MethodInfo putdata = Engine.GetMethod("PutData");

int rc = 0;
Type enumType = WorkerDLL.GetType("MyNameSpace.InputFields");
System.Reflection.FieldInfo enumItem = enumType.GetField("Version");
int x = (int)enumItem.GetValue(enumType);
// package our parameters
object[] arrParms = new object[3];
arrParms.SetValue(enumItem, 0);
arrParms.SetValue(i, 1);
arrParms.SetValue(sData, 2);
rc = (int)getdata.Invoke(myEngine, arrParms);





//My DLL


namespace MyNameSpace
{

public enum InputFields
{
Family,
Version,
Width,
Size,
}




public class Worker
{

public Worker()
{}

public int PutData(object field, int occurrence, string data)
{
int rc = 0;

//CAnnot get match here
if (field.GetType() == typeof(InputFields))
{

}

return(rc);
}

}
}
 
S

sippyuconn

Thanks for the info

The answer to you question is my original question
From Reflection I know I am getting a Type of FieldInfo

I am looking for the way to create the Object as Type
"InputFields" thru reflection ??




Peter Duniho said:
[incomplete, non-concise code example snipped]

Here are some links you may find helpful:
http://www.yoda.arachsys.com/csharp/complete.html
http://www.yoda.arachsys.com/csharp/incomplete.html
http://sscce.org/ (some Java-centric stuff, but mostly applicable to any
programming questions)

And the previous question still remains: you are explicitly passing an
instance of FieldInfo. Why in the world would you expect the type to be
anything except tyepof(FieldInfo)?
 
S

sippyuconn

Thanks for the info

The answer to you question is my original question
From Reflection I know I am getting a Type of FieldInfo

I am looking for the way to create the Object as Type
"InputFields" thru reflection ??




Peter Duniho said:
[incomplete, non-concise code example snipped]

Here are some links you may find helpful:
http://www.yoda.arachsys.com/csharp/complete.html
http://www.yoda.arachsys.com/csharp/incomplete.html
http://sscce.org/ (some Java-centric stuff, but mostly applicable to any
programming questions)

And the previous question still remains: you are explicitly passing an
instance of FieldInfo. Why in the world would you expect the type to be
anything except tyepof(FieldInfo)?
 
P

Pavel Minaev

Using Reflection to get an Enumeration value that I pass to another DLL
The dll has a check of incoming Enum paramater is of specific type

The checl always fails because it thinks the Enum if of Type FieldInfo
Is there a way to cast to Original type ???

Thanks

//Reflection to get Enum and pass to Dll Method

Type enumType = GenericDLL.GetType("MyTest.InputFields");
 System.Reflection.FieldInfo enumItem = enumType.GetField("Enum1");

Method1(enumItem );

//Dll method

Method1(object o1)
{
            //This method always fails - gets type FieldInfo
            if (o1 .GetType() == typeof(InputFields))
                MessageBox.Show("OK");
}

When called on _any_ object, GetType() returns the type of that
object. For a string, it will give you typeof(string). And for an
instance of FieldInfo, it will give you typeof(FieldInfo).

If you have a FieldInfo, and want to get the declared type of the
corresponding field, you should use FieldInfo.FieldType property.
 
P

Pavel Minaev

Using Reflection to get an Enumeration value that I pass to another DLL
The dll has a check of incoming Enum paramater is of specific type

The checl always fails because it thinks the Enum if of Type FieldInfo
Is there a way to cast to Original type ???

Thanks

//Reflection to get Enum and pass to Dll Method

Type enumType = GenericDLL.GetType("MyTest.InputFields");
 System.Reflection.FieldInfo enumItem = enumType.GetField("Enum1");

Method1(enumItem );

//Dll method

Method1(object o1)
{
            //This method always fails - gets type FieldInfo
            if (o1 .GetType() == typeof(InputFields))
                MessageBox.Show("OK");
}

When called on _any_ object, GetType() returns the type of that
object. For a string, it will give you typeof(string). And for an
instance of FieldInfo, it will give you typeof(FieldInfo).

If you have a FieldInfo, and want to get the declared type of the
corresponding field, you should use FieldInfo.FieldType property.
 
P

Paul

Try this.....add this after setting x.

object enumValue= Enum.ToObject(enumType, x);

change this

'arrParms.SetValue(enumItem, 0);'

to

arrParms.SetValue(enumValue, 0);


What Peter was telling you was correct you were creating a method call in
reflection that looked something like this.

PutData(enumItem, i, sData)

but enumitem is a fieldinfo object which is fine, but then this

if (field.GetType() == typeof(InputFields))

would always be false.

I have not tested the above but don't think its far off.


sippyuconn said:
//Test EXE Calls DLL


//Setup Assembly we need thru Reflection
string sPath =
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) +
"\\Worker.dll";
Assembly WorkerDLL = Assembly.LoadFile(sPath);
// Get the type of the needed object:
Type Engine = WorkerDLL.GetType("MyNameSpace.Worker");
// Create an instance of the needed object:
object myEngine = Activator.CreateInstance(Engine);


MethodInfo putdata = Engine.GetMethod("PutData");

int rc = 0;
Type enumType = WorkerDLL.GetType("MyNameSpace.InputFields");
System.Reflection.FieldInfo enumItem = enumType.GetField("Version");
int x = (int)enumItem.GetValue(enumType);
// package our parameters
object[] arrParms = new object[3];
arrParms.SetValue(enumItem, 0);
arrParms.SetValue(i, 1);
arrParms.SetValue(sData, 2);
rc = (int)getdata.Invoke(myEngine, arrParms);





//My DLL


namespace MyNameSpace
{

public enum InputFields
{
Family,
Version,
Width,
Size,
}




public class Worker
{

public Worker()
{}

public int PutData(object field, int occurrence, string data)
{
int rc = 0;

//CAnnot get match here
if (field.GetType() == typeof(InputFields))
{

}

return(rc);
}

}
}







Peter Duniho said:
You're passing an instance of FieldInfo to the method. Why _shouldn't_
the type be FieldInfo?

Post a concise-but-complete code example, and perhaps we can help.
Without such an example, it's hard to know what you're even trying to do,
never mind the best way to solve it.
 
P

Paul

Try this.....add this after setting x.

object enumValue= Enum.ToObject(enumType, x);

change this

'arrParms.SetValue(enumItem, 0);'

to

arrParms.SetValue(enumValue, 0);


What Peter was telling you was correct you were creating a method call in
reflection that looked something like this.

PutData(enumItem, i, sData)

but enumitem is a fieldinfo object which is fine, but then this

if (field.GetType() == typeof(InputFields))

would always be false.

I have not tested the above but don't think its far off.


sippyuconn said:
//Test EXE Calls DLL


//Setup Assembly we need thru Reflection
string sPath =
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) +
"\\Worker.dll";
Assembly WorkerDLL = Assembly.LoadFile(sPath);
// Get the type of the needed object:
Type Engine = WorkerDLL.GetType("MyNameSpace.Worker");
// Create an instance of the needed object:
object myEngine = Activator.CreateInstance(Engine);


MethodInfo putdata = Engine.GetMethod("PutData");

int rc = 0;
Type enumType = WorkerDLL.GetType("MyNameSpace.InputFields");
System.Reflection.FieldInfo enumItem = enumType.GetField("Version");
int x = (int)enumItem.GetValue(enumType);
// package our parameters
object[] arrParms = new object[3];
arrParms.SetValue(enumItem, 0);
arrParms.SetValue(i, 1);
arrParms.SetValue(sData, 2);
rc = (int)getdata.Invoke(myEngine, arrParms);





//My DLL


namespace MyNameSpace
{

public enum InputFields
{
Family,
Version,
Width,
Size,
}




public class Worker
{

public Worker()
{}

public int PutData(object field, int occurrence, string data)
{
int rc = 0;

//CAnnot get match here
if (field.GetType() == typeof(InputFields))
{

}

return(rc);
}

}
}







Peter Duniho said:
You're passing an instance of FieldInfo to the method. Why _shouldn't_
the type be FieldInfo?

Post a concise-but-complete code example, and perhaps we can help.
Without such an example, it's hard to know what you're even trying to do,
never mind the best way to solve it.
 
L

Leon Lambert

Use FieldType property on FieldInfo instance to get the actual type of
the field.

Hope this helps
Leon Lambert
//Test EXE Calls DLL


//Setup Assembly we need thru Reflection
string sPath =
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) +
"\\Worker.dll";
Assembly WorkerDLL = Assembly.LoadFile(sPath);
// Get the type of the needed object:
Type Engine = WorkerDLL.GetType("MyNameSpace.Worker");
// Create an instance of the needed object:
object myEngine = Activator.CreateInstance(Engine);


MethodInfo putdata = Engine.GetMethod("PutData");

int rc = 0;
Type enumType = WorkerDLL.GetType("MyNameSpace.InputFields");
System.Reflection.FieldInfo enumItem = enumType.GetField("Version");
int x = (int)enumItem.GetValue(enumType);
// package our parameters
object[] arrParms = new object[3];
arrParms.SetValue(enumItem, 0);
arrParms.SetValue(i, 1);
arrParms.SetValue(sData, 2);
rc = (int)getdata.Invoke(myEngine, arrParms);





//My DLL


namespace MyNameSpace
{

public enum InputFields
{
Family,
Version,
Width,
Size,
}




public class Worker
{

public Worker()
{}

public int PutData(object field, int occurrence, string data)
{
int rc = 0;

//CAnnot get match here
if (field.GetType() == typeof(InputFields))
{

}

return(rc);
}

}
}







Peter Duniho said:
You're passing an instance of FieldInfo to the method. Why _shouldn't_
the type be FieldInfo?

Post a concise-but-complete code example, and perhaps we can help.
Without such an example, it's hard to know what you're even trying to do,
never mind the best way to solve it.
 
L

Leon Lambert

Use FieldType property on FieldInfo instance to get the actual type of
the field.

Hope this helps
Leon Lambert
//Test EXE Calls DLL


//Setup Assembly we need thru Reflection
string sPath =
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) +
"\\Worker.dll";
Assembly WorkerDLL = Assembly.LoadFile(sPath);
// Get the type of the needed object:
Type Engine = WorkerDLL.GetType("MyNameSpace.Worker");
// Create an instance of the needed object:
object myEngine = Activator.CreateInstance(Engine);


MethodInfo putdata = Engine.GetMethod("PutData");

int rc = 0;
Type enumType = WorkerDLL.GetType("MyNameSpace.InputFields");
System.Reflection.FieldInfo enumItem = enumType.GetField("Version");
int x = (int)enumItem.GetValue(enumType);
// package our parameters
object[] arrParms = new object[3];
arrParms.SetValue(enumItem, 0);
arrParms.SetValue(i, 1);
arrParms.SetValue(sData, 2);
rc = (int)getdata.Invoke(myEngine, arrParms);





//My DLL


namespace MyNameSpace
{

public enum InputFields
{
Family,
Version,
Width,
Size,
}




public class Worker
{

public Worker()
{}

public int PutData(object field, int occurrence, string data)
{
int rc = 0;

//CAnnot get match here
if (field.GetType() == typeof(InputFields))
{

}

return(rc);
}

}
}







Peter Duniho said:
You're passing an instance of FieldInfo to the method. Why _shouldn't_
the type be FieldInfo?

Post a concise-but-complete code example, and perhaps we can help.
Without such an example, it's hard to know what you're even trying to do,
never mind the best way to solve it.
 
J

Jie Wang [MSFT]

Hi,

Leon and Pavel were right, the FieldType property is the right way to go.

So your line should be:

if (field.FieldType == typeof(InputFields))
{
....
}

For more information on the FieldType property, please refer to the
following page on MSDN:
http://msdn.microsoft.com/en-us/library/system.reflection.fieldinfo.fieldtyp
e.aspx

Regards,

Jie Wang ([email protected], remove 'online.')

Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business days is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jie Wang [MSFT]

Hi,

Leon and Pavel were right, the FieldType property is the right way to go.

So your line should be:

if (field.FieldType == typeof(InputFields))
{
....
}

For more information on the FieldType property, please refer to the
following page on MSDN:
http://msdn.microsoft.com/en-us/library/system.reflection.fieldinfo.fieldtyp
e.aspx

Regards,

Jie Wang ([email protected], remove 'online.')

Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business days is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jie Wang [MSFT]

Hi,

Is the problem solved?

If you have any further questions regarding this issue, please feel free to
post here.

Regards,

Jie Wang ([email protected], remove 'online.')

Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business days is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jie Wang [MSFT]

Hi,

Is the problem solved?

If you have any further questions regarding this issue, please feel free to
post here.

Regards,

Jie Wang ([email protected], remove 'online.')

Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business days is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top