示例#1
0
 /// <summary>
 /// 创建随机枚举值
 /// </summary>
 /// <returns></returns>
 private static valueType enumULong()
 {
     return(Emit.EnumCast <valueType, ulong> .FromInt(MethodCache.CreateULong()));
 }
示例#2
0
        static Creator()
        {
            Type       type   = typeof(valueType);
            MethodInfo method = MethodCache.GetMethod(type);

            if (method != null)
            {
                defaultCreator = (Func <valueType>)Delegate.CreateDelegate(typeof(Func <valueType>), method);
                return;
            }
            if ((method = MethodCache.GetConfigMethod(type)) != null)
            {
                configCreator     = (Func <Config, valueType>)Delegate.CreateDelegate(typeof(Func <Config, valueType>), method);
                configNullCreator = (Func <Config, valueType>)Delegate.CreateDelegate(typeof(Func <Config, valueType>), MethodCache.GetConfigNullMethod(type));
                return;
            }
            if (type.IsArray)
            {
                if (type.GetArrayRank() == 1)
                {
                    Type elementType = type.GetElementType();
                    configNullCreator = (Func <Config, valueType>)Delegate.CreateDelegate(typeof(Func <Config, valueType>), MethodCache.CreateArrayNullMethod.MakeGenericMethod(elementType));
                    configCreator     = (Func <Config, valueType>)Delegate.CreateDelegate(typeof(Func <Config, valueType>), MethodCache.CreateArrayMethod.MakeGenericMethod(elementType));
                }
                return;
            }
            if (type.IsEnum)
            {
                Type enumType = System.Enum.GetUnderlyingType(type);
                if (enumType == typeof(uint))
                {
                    defaultCreator = enumUInt;
                }
                else if (enumType == typeof(byte))
                {
                    defaultCreator = enumByte;
                }
                else if (enumType == typeof(ulong))
                {
                    defaultCreator = enumULong;
                }
                else if (enumType == typeof(ushort))
                {
                    defaultCreator = enumUShort;
                }
                else if (enumType == typeof(long))
                {
                    defaultCreator = enumLong;
                }
                else if (enumType == typeof(short))
                {
                    defaultCreator = enumShort;
                }
                else if (enumType == typeof(sbyte))
                {
                    defaultCreator = enumSByte;
                }
                else
                {
                    defaultCreator = enumInt;
                }
                return;
            }
            if (type.IsGenericType)
            {
                Type genericType = type.GetGenericTypeDefinition();
                if (genericType == typeof(Nullable <>))
                {
                    configNullCreator = configCreator = (Func <Config, valueType>)Delegate.CreateDelegate(typeof(Func <Config, valueType>), MethodCache.CreateNullableMethod.MakeGenericMethod(type.GetGenericArguments()));
                    return;
                }
                if (genericType == typeof(LeftArray <>))
                {
                    configNullCreator = configCreator = (Func <Config, valueType>)Delegate.CreateDelegate(typeof(Func <Config, valueType>), MethodCache.CreateLeftArrayMethod.MakeGenericMethod(type.GetGenericArguments()));
                    return;
                }
                if (genericType == typeof(ListArray <>))
                {
                    Type[] parameterTypes = type.GetGenericArguments();
                    configNullCreator = (Func <Config, valueType>)Delegate.CreateDelegate(typeof(Func <Config, valueType>), MethodCache.CreateListArrayNullMethod.MakeGenericMethod(parameterTypes));
                    configCreator     = (Func <Config, valueType>)Delegate.CreateDelegate(typeof(Func <Config, valueType>), MethodCache.CreateListArrayMethod.MakeGenericMethod(parameterTypes));
                    return;
                }
                if (genericType == typeof(List <>))
                {
                    Type[] parameterTypes = type.GetGenericArguments();
                    configNullCreator = (Func <Config, valueType>)Delegate.CreateDelegate(typeof(Func <Config, valueType>), MethodCache.CreateListNullMethod.MakeGenericMethod(parameterTypes));
                    configCreator     = (Func <Config, valueType>)Delegate.CreateDelegate(typeof(Func <Config, valueType>), MethodCache.CreateListMethod.MakeGenericMethod(parameterTypes));
                    return;
                }
#if DOTNET2
                if (genericType == typeof(Queue <>) || genericType == typeof(Stack <>) || genericType == typeof(LinkedList <>))
#else
                if (genericType == typeof(HashSet <>) || genericType == typeof(Queue <>) || genericType == typeof(Stack <>) || genericType == typeof(SortedSet <>) || genericType == typeof(LinkedList <>))
#endif
                {
                    Type[] parameterTypes = new Type[] { type, type.GetGenericArguments()[0] };
                    configNullCreator = (Func <Config, valueType>)Delegate.CreateDelegate(typeof(Func <Config, valueType>), MethodCache.CreateEnumerableConstructorNullMethod.MakeGenericMethod(parameterTypes));
                    configCreator     = (Func <Config, valueType>)Delegate.CreateDelegate(typeof(Func <Config, valueType>), MethodCache.CreateEnumerableConstructorMethod.MakeGenericMethod(parameterTypes));
                    return;
                }
                if (genericType == typeof(Dictionary <,>))
                {
                    Type[] parameterTypes = type.GetGenericArguments();
                    configNullCreator = (Func <Config, valueType>)Delegate.CreateDelegate(typeof(Func <Config, valueType>), MethodCache.CreateDictionaryNullMethod.MakeGenericMethod(parameterTypes));
                    configCreator     = (Func <Config, valueType>)Delegate.CreateDelegate(typeof(Func <Config, valueType>), MethodCache.CreateDictionaryMethod.MakeGenericMethod(parameterTypes));
                    return;
                }
                if (genericType == typeof(SortedDictionary <,>))
                {
                    Type[] parameterTypes = type.GetGenericArguments();
                    configNullCreator = (Func <Config, valueType>)Delegate.CreateDelegate(typeof(Func <Config, valueType>), MethodCache.CreateSortedDictionaryNullMethod.MakeGenericMethod(parameterTypes));
                    configCreator     = (Func <Config, valueType>)Delegate.CreateDelegate(typeof(Func <Config, valueType>), MethodCache.CreateSortedDictionaryMethod.MakeGenericMethod(parameterTypes));
                    return;
                }
                if (genericType == typeof(SortedList <,>))
                {
                    Type[] parameterTypes = type.GetGenericArguments();
                    configNullCreator = (Func <Config, valueType>)Delegate.CreateDelegate(typeof(Func <Config, valueType>), MethodCache.CreateSortedListNullMethod.MakeGenericMethod(parameterTypes));
                    configCreator     = (Func <Config, valueType>)Delegate.CreateDelegate(typeof(Func <Config, valueType>), MethodCache.CreateSortedListMethod.MakeGenericMethod(parameterTypes));
                    return;
                }
            }
            if (type.IsPointer || type.IsInterface)
            {
                return;
            }
            isValueType = type.IsValueType;
#if NOJIT
            MemberCreator = new memberRandom().Random();
#else
            MemberDynamicMethod dynamicMethod = new MemberDynamicMethod(type);
            foreach (FieldInfo field in type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public))// | BindingFlags.DeclaredOnly
            {
                dynamicMethod.Push(field);
            }
            dynamicMethod.Base();
            try
            {
                MemberCreator = (creator)dynamicMethod.Create <creator>();
            }
            catch (Exception error)
            {
                Console.WriteLine(error.ToString());
            }
#endif
        }
示例#3
0
 /// <summary>
 /// 创建随机枚举值
 /// </summary>
 /// <returns></returns>
 private static valueType enumShort()
 {
     return(Emit.EnumCast <valueType, short> .FromInt(MethodCache.CreateShort()));
 }
示例#4
0
 /// <summary>
 /// 创建随机枚举值
 /// </summary>
 /// <returns></returns>
 private static valueType enumUInt()
 {
     return(Emit.EnumCast <valueType, uint> .FromInt(MethodCache.CreateUInt()));
 }
示例#5
0
 /// <summary>
 /// 创建随机枚举值
 /// </summary>
 /// <returns></returns>
 private static valueType enumSByte()
 {
     return(Emit.EnumCast <valueType, sbyte> .FromInt(MethodCache.CreateSByte()));
 }
示例#6
0
 internal static T EnumShort <T>()
 {
     return(AutoCSer.Metadata.EnumCast <T, short> .FromInt(MethodCache.CreateShort()));
 }
示例#7
0
 internal static T EnumUInt <T>()
 {
     return(AutoCSer.Metadata.EnumCast <T, uint> .FromInt(MethodCache.CreateUInt()));
 }
示例#8
0
 internal static T EnumLong <T>()
 {
     return(AutoCSer.Metadata.EnumCast <T, long> .FromInt(MethodCache.CreateLong()));
 }
示例#9
0
 internal static T EnumSByte <T>()
 {
     return(AutoCSer.Metadata.EnumCast <T, sbyte> .FromInt(MethodCache.CreateSByte()));
 }