public static Sigil.NonGeneric.Emit Box(this Sigil.NonGeneric.Emit emitter, Type typeFrom, bool castIfRefType) { if (typeFrom.IsByRef) { var baseType = typeFrom.GetElementType(); if( baseType.IsPrimitive) return emitter.LoadIndirect(baseType).Box(baseType); if( baseType.IsValueType) return emitter.LoadObject(baseType).Box(baseType); if (!baseType.IsValueType) return emitter.LoadIndirect(baseType); throw new NotSupportedException(typeFrom.FullName + " not supported in this context"); } if (!typeFrom.IsValueType) return emitter.CastClass(typeFrom); return emitter.Box(typeFrom); }
public static Match Else(this Match.IError data, IMatch other) => data.Box().Else(other);
/// <summary> /// /// </summary> /// <param name="il"></param> /// <param name="source"></param> /// <param name="destination"></param> /// <returns></returns> public static ILGenerator Convert(this ILGenerator il, Type source, Type destination) { if (source == null) throw new ArgumentNullException("source"); if (destination == null) throw new ArgumentNullException("destination"); if (source == destination) return il; if (source == typeof(Object) && destination.IsValueType) return il.Unbox_Any(destination); if (source.IsValueType && destination == typeof(Object)) return il.Box(destination); // if (source.IsAssignableFrom(destination)) return this; // --> it doesn't work for int? -> int, cause int is assignable from int? var converter = LookUpConverter(source, destination); if (converter != null) // not so beauty, but it's enough for internal code { // todo. implement invariant culture here if (converter is ConstructorInfo) return il.Newobj((ConstructorInfo)converter); // note the ClassCastException expected below in near future :) return converter.IsVirtual ? il.Callvirt((MethodInfo)converter) : il.Call((MethodInfo)converter); } Func<ILGenerator, ILGenerator> emitter; if (CanGenerateConverter(source, destination, out emitter)) return emitter(il); return il.Castclass(destination); }
public static Match Else(this string data, IMatch other) => data.Box().Else(other);