GetFieldValue throws not supported method with enum Type
description
Hi guys,
first I want to thank you for this great piece of code!
I'm using the latest Fasterflect library inside an experimental project to make available the clr to the lua (www.lua.org) scripting language like the luainterface project (http://code.google.com/p/luainterface/).
I'm calling GetFieldValue on an enum type, passing one field name as argument. I expect the field value as return object but an exception is thrown. I'm not sure if this is the right way to accomplish that job, if not so, please forgive me: this is my first project using C#.
Follows some code to reproduce the "problem": testDirect function uses standard reflection while testFasterflect uses fasterflect's GetFieldValue api.
//---------------------- source -------------------------
using System;
using System.Reflection;
using Fasterflect;
public enum myenum
{
one = 1,
two = 2,
four = 4
}
namespace EnumProblem
{
class Program
{
static void Main(string[] args)
{
testDirect();
testFasterflect();
Console.ReadLine();
}
static void testDirect()
{
Console.WriteLine("** direct test **");
Assembly a = Assembly.GetExecutingAssembly();
Type te = a.GetType("myenum");
if (te != null)
{
Console.WriteLine(te.ToString());
FieldInfo fi = te.GetField("two");
if (fi != null)
{
Console.WriteLine(fi.ToString());
object v = fi.GetValue(te);
if (v != null)
{
Console.WriteLine(v.ToString());
}
}
}
}
static void testFasterflect()
{
Console.WriteLine("** fasterflect test **");
Assembly a = Assembly.GetExecutingAssembly();
Type te = a.GetType("myenum");
if (te != null)
{
Console.WriteLine(te.ToString());
object v = null;
try
{
v = te.GetFieldValue("two");
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
if (v != null)
{
Console.WriteLine(v.ToString());
}
}
}
}
}
//---------------------- output -------------------------
** direct test **
myenum
myenum two
two
** fasterflect test **
myenum
System.NotSupportedException: Specified method is not supported.
at System.Reflection.MdFieldInfo.get_FieldHandle()
at System.Reflection.Emit.DynamicILGenerator.Emit(OpCode opcode, FieldInfo field)
at Fasterflect.Emitter.EmitHelper.ldfld(Boolean isStatic, FieldInfo fieldInfo) in ...\Fasterflect\Emitter\EmitHelper.cs:line 265
at Fasterflect.Emitter.MemberGetEmitter.CreateDelegate() in ...\Fasterflect\Emitter\MemberGetEmitter.cs:line 78
at Fasterflect.Emitter.BaseEmitter.GetDelegate() in ...\Fasterflect\Emitter\BaseEmitter.cs:line 50
at Fasterflect.FieldExtensions.DelegateForGetFieldValue(Type type, String name, Flags bindingFlags) in ...\Fasterflect\Extensions\Core\FieldExtensions.cs:line 107
at Fasterflect.FieldExtensions.DelegateForGetFieldValue(Type type, String name) in ...\Fasterflect\Extensions\Core\FieldExtensions.cs:line 87
at Fasterflect.FieldExtensions.GetFieldValue(Object obj, String name) in ...\Fasterflect\Extensions\Core\FieldExtensions.cs:line 49
at EnumProblem.Program.testFasterflect() in ...\EnumProblem\Program.cs:line 62
//---------------------- end -------------------------
Thank you for any help
Regards, Fabio