diff options
| author | Jean-Philippe Bruyère <jp_bruyere@hotmail.com> | 2020-04-21 15:43:45 +0200 |
|---|---|---|
| committer | j-p <jp_bruyere@hotmail.com> | 2020-05-10 00:50:02 +0200 |
| commit | 6b4d2ea66d5312a2494d49beb6d868da68f11e9d (patch) | |
| tree | f12935ef582d7ebb8e7acbb308930a090025169b | |
| parent | e320afaf6485241c6cce5a4cbe367f17b85ba412 (diff) | |
parse enum in binding conversion if destType is Enum and sourceType is string
| -rw-r--r-- | Crow/src/IML/CompilerServices.cs | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/Crow/src/IML/CompilerServices.cs b/Crow/src/IML/CompilerServices.cs index 6ea3c957..a66a79c1 100644 --- a/Crow/src/IML/CompilerServices.cs +++ b/Crow/src/IML/CompilerServices.cs @@ -33,6 +33,7 @@ namespace Crow.IML internal static MethodInfo miGetType = typeof(object).GetMethod("GetType"); internal static MethodInfo miParseEnum = typeof(Enum).GetMethod("Parse", BindingFlags.Static | BindingFlags.Public, Type.DefaultBinder, new Type [] {typeof (Type), typeof (string), typeof (bool)}, null); + internal static MethodInfo miParseEnumInversedParams = typeof(CompilerServices).GetMethod ("ParseEnum", BindingFlags.Static | BindingFlags.NonPublic); internal static MethodInfo miGetTypeFromHandle = typeof(Type).GetMethod("GetTypeFromHandle"); internal static MethodInfo miGetEvent = typeof(Type).GetMethod("GetEvent", new Type[] {typeof(string)}); @@ -560,9 +561,17 @@ namespace Crow.IML MethodInfo miParse = destType.GetMethod ("Parse", BindingFlags.Static | BindingFlags.Public, Type.DefaultBinder, new Type [] { typeof (string) }, null); - if (miParse == null) - throw new Exception ("no Parse method found for: " + destType.FullName); - il.Emit (OpCodes.Call, miParse); + if (miParse == null) { + //TODO:find parse for enums destTypes + if (destType.IsEnum) { + il.Emit (OpCodes.Ldtoken, destType);//push destination property type for testing + il.Emit (OpCodes.Call, CompilerServices.miGetTypeFromHandle); + il.Emit (OpCodes.Call, CompilerServices.miParseEnumInversedParams); + il.Emit (OpCodes.Unbox_Any, destType); + } else + throw new Exception ("no Parse method found for: " + destType.FullName); + }else + il.Emit (OpCodes.Call, miParse); } //implicit conversion can't be defined from or to object base class, //so we will check if object underlying type is one of the implicit converter of destType @@ -598,6 +607,8 @@ namespace Crow.IML } } } + internal static object ParseEnum (string str, Type enumType) => Enum.Parse (enumType, str); + internal static bool isValueType (object obj) => obj.GetType ().IsValueType; /// <summary> /// check type of current object on the stack and convert to dest type, |