示例#1
0
        private static MetaInformation GetOrCreateMetaInfo(Type typeTo)
        {
            var found = MetaRepository.GetOrDefault(typeTo);

            if (found != null)
            {
                return(found);
            }

            var meta = new MetaInformation(typeTo);

            MetaRepository.AddOrUpdate(typeTo, meta);
            return(meta);
        }
示例#2
0
        private static Type GetOrAddTargetType(Constants constants)
        {
            var length = constants.ConstantExpressions.Count;
            var types  = new Type[length];

            var foundType = TargetTypes.GetOrDefault(length);

            if (foundType != null)
            {
                if (length <= 0)
                {
                    return(foundType);
                }

                for (var i = 0; i < length; i++)
                {
                    types[i] = constants.ConstantExpressions[i].Type;
                }

                return(foundType.MakeGenericType(types));
            }

            var fields      = new FieldInfo[length];
            var typeBuilder = ExpressionEmitter.ModuleBuilder.Value.DefineType("DT" + Interlocked.Increment(ref typeCounter), TypeAttributes.Public);

            if (length > 0)
            {
                var typeParamNames = new string[length];
                for (var i = 0; i < length; i++)
                {
                    typeParamNames[i] = "T" + i;
                }

                var typeParams = typeBuilder.DefineGenericParameters(typeParamNames);
#if NETSTANDARD1_3
                var genericTypes = new Type[length];
#endif

                for (var i = 0; i < length; i++)
                {
                    types[i] = constants.ConstantExpressions[i].Type;
#if NETSTANDARD1_3
                    var genericType = typeParams[i].AsType();
                    genericTypes[i] = genericType;
                    fields[i]       = typeBuilder.DefineField("F" + i, genericType, FieldAttributes.Public);
#else
                    fields[i] = typeBuilder.DefineField("F" + i, typeParams[i], FieldAttributes.Public);
#endif
                }

#if NETSTANDARD1_3
                var constructor = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.HasThis, genericTypes);
#else
                var constructor = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.HasThis, typeParams);
#endif
                var generator = constructor.GetILGenerator();

                generator.Emit(OpCodes.Ldarg_0);
                generator.Emit(OpCodes.Call, Stashbox.Constants.ObjectConstructor);

                for (var i = 0; i < length; i++)
                {
                    generator.Emit(OpCodes.Ldarg_0);
                    generator.LoadParameter(i + 1);
                    generator.Emit(OpCodes.Stfld, fields[i]);
                }

                generator.Emit(OpCodes.Ret);
            }
#if NETSTANDARD1_3
            var type = typeBuilder.CreateTypeInfo().AsType();
#else
            var type = typeBuilder.CreateType();
#endif
            TargetTypes.AddOrUpdate(length, type);
            return(length > 0 ? type.MakeGenericType(types) : type);
        }