public static bool IsSupported(Type type)
 {
     lock (_supported)
     {
         return(_supported.Contains(type) || type.IsArray || !ReflectionAdapter.IsClass(type));
     }
 }
示例#2
0
 internal static string GetSqlName(DbDefinitionCustomizerDelegate defineCustomizer, PropertyInfo property)
 {
     if (defineCustomizer != null)
     {
         var name = defineCustomizer(property);
         if (!string.IsNullOrEmpty(name))
         {
             return(name);
         }
     }
     return(ReflectionAdapter.GetSqlName(property));
 }
示例#3
0
        public void LoadTest()
        {
            var configuration = new ReflectionAdapter(new DotNetConfigurationStore()
            {
                FileName = "ConfigKramLoadTest.config"
            });
            var test = configuration.Load <TestConfigurationObject>();

            Assert.Equal("default", test.TestString);
            Assert.Equal(10, test.TestInt);
            Assert.Null(test.TestNullableInt);
        }
示例#4
0
        public void SaveTest()
        {
            var configuration = new ReflectionAdapter(new DotNetConfigurationStore()
            {
                FileName = "ConfigKramSaveTest.config"
            });
            var test = new TestConfigurationObject {
                TestString = nameof(SaveTest), TestInt = -1, TestNullableInt = null
            };

            configuration.Save(test);

            test = new TestConfigurationObject();
            configuration.LoadInto(test);

            Assert.Equal(nameof(SaveTest), test.TestString);
            Assert.Equal(-1, test.TestInt);
            Assert.Null(test.TestNullableInt);
        }
示例#5
0
        private IList <EdmSchemaError> InternalGenerateCode(string sourceEdmSchemaFilePath, Version schemaVersion, LazyTextWriterCreator textWriter, IEnumerable <string> additionalEdmSchemaFilePaths, Version targetFrameworkVersion)
        {
            List <EdmSchemaError> errors = new List <EdmSchemaError>();

            try
            {
                if (targetFrameworkVersion == EntityFrameworkVersions.Version1)
                {
                    errors.Add(new EdmSchemaError(Strings.EntityCodeGenTargetTooLow, (int)ModelBuilderErrorCode.TargetVersionNotSupported, EdmSchemaErrorSeverity.Error));
                    return(errors);
                }

                if (!MetadataItemCollectionFactory.ValidateActualVersionAgainstTarget(targetFrameworkVersion, schemaVersion, errors))
                {
                    return(errors);
                }

                if (schemaVersion == EntityFrameworkVersions.EdmVersion1_1)
                {
                    return(GenerateCodeFor1_1Schema(sourceEdmSchemaFilePath, textWriter, additionalEdmSchemaFilePaths));
                }

                ReflectionAdapter codeGenerator = ReflectionAdapter.Create(_languageOption, targetFrameworkVersion);
                codeGenerator.SourceCsdlPath          = sourceEdmSchemaFilePath;
                codeGenerator.ReferenceCsdlPaths      = additionalEdmSchemaFilePaths;
                codeGenerator.EdmToObjectNamespaceMap = _edmToObjectNamespaceMap.AsDictionary();

                string code = codeGenerator.TransformText();

                if (codeGenerator.Errors.Count != 0)
                {
                    ModelBuilderErrorCode errorCode = ModelBuilderErrorCode.PreprocessTemplateTransformationError;
                    errors.AddRange(codeGenerator.Errors.OfType <CompilerError>().Select(c => ConvertToEdmSchemaError(c, errorCode)));

                    if (codeGenerator.Errors.HasErrors)
                    {
                        return(errors);
                    }
                }

                using (TextWriter writer = textWriter.GetOrCreateTextWriter())
                {
                    writer.Write(code);
                }
            }
            catch (System.UnauthorizedAccessException ex)
            {
                errors.Add(EntityClassGenerator.CreateErrorForException(ModelBuilderErrorCode.SecurityError, ex));
            }
            catch (System.IO.FileNotFoundException ex)
            {
                errors.Add(EntityClassGenerator.CreateErrorForException(ModelBuilderErrorCode.FileNotFound, ex));
            }
            catch (System.Security.SecurityException ex)
            {
                errors.Add(EntityClassGenerator.CreateErrorForException(ModelBuilderErrorCode.SecurityError, ex));
            }
            catch (System.IO.DirectoryNotFoundException ex)
            {
                errors.Add(EntityClassGenerator.CreateErrorForException(ModelBuilderErrorCode.DirectoryNotFound, ex));
            }
            catch (System.IO.IOException ex)
            {
                errors.Add(EntityClassGenerator.CreateErrorForException(ModelBuilderErrorCode.IOException, ex));
            }
            catch (Exception e)
            {
                if (MetadataUtil.IsCatchableExceptionType(e))
                {
                    errors.Add(EntityClassGenerator.CreateErrorForException(ModelBuilderErrorCode.PreprocessTemplateTransformationError, e, sourceEdmSchemaFilePath));
                }
                else
                {
                    throw;
                }
            }

            return(errors);
        }
示例#6
0
        static IGetter CreateGetter(Type[] args)
        {
            switch (args.Length)
            {
            case 0: return(ReflectionAdapter.CreateInstance(typeof(GetterCore), true) as IGetter);

            case 1: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <>).MakeGenericType(args), true) as IGetter);

            case 2: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <,>).MakeGenericType(args), true) as IGetter);

            case 3: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, ,>).MakeGenericType(args), true) as IGetter);

            case 4: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , ,>).MakeGenericType(args), true) as IGetter);

            case 5: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , , ,>).MakeGenericType(args), true) as IGetter);

            case 6: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , , , ,>).MakeGenericType(args), true) as IGetter);

            case 7: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , , , , ,>).MakeGenericType(args), true) as IGetter);

            case 8: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , , , , , ,>).MakeGenericType(args), true) as IGetter);

            case 9: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , , , , , , ,>).MakeGenericType(args), true) as IGetter);

            case 10: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , , , , , , , ,>).MakeGenericType(args), true) as IGetter);

            case 11: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , , , , , , , , ,>).MakeGenericType(args), true) as IGetter);

            case 12: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , , , , , , , , , ,>).MakeGenericType(args), true) as IGetter);

            case 13: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , , , , , , , , , , ,>).MakeGenericType(args), true) as IGetter);

            case 14: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , , , , , , , , , , , ,>).MakeGenericType(args), true) as IGetter);

            case 15: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , , , , , , , , , , , , ,>).MakeGenericType(args), true) as IGetter);

            case 16: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , , , , , , , , , , , , , ,>).MakeGenericType(args), true) as IGetter);

            case 17: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , , , , , , , , , , , , , , ,>).MakeGenericType(args), true) as IGetter);

            case 18: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , , , , , , , , , , , , , , , ,>).MakeGenericType(args), true) as IGetter);

            case 19: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , , , , , , , , , , , , , , , , ,>).MakeGenericType(args), true) as IGetter);

            case 20: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , , , , , , , , , , , , , , , , , ,>).MakeGenericType(args), true) as IGetter);

            case 21: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , , , , , , , , , , , , , , , , , , ,>).MakeGenericType(args), true) as IGetter);

            case 22: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , , , , , , , , , , , , , , , , , , , ,>).MakeGenericType(args), true) as IGetter);

            case 23: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , , , , , , , , , , , , , , , , , , , , ,>).MakeGenericType(args), true) as IGetter);

            case 24: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , , , , , , , , , , , , , , , , , , , , , ,>).MakeGenericType(args), true) as IGetter);

            case 25: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , , , , , , , , , , , , , , , , , , , , , , ,>).MakeGenericType(args), true) as IGetter);

            case 26: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , , , , , , , , , , , , , , , , , , , , , , , ,>).MakeGenericType(args), true) as IGetter);

            case 27: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , , , , , , , , , , , , , , , , , , , , , , , , ,>).MakeGenericType(args), true) as IGetter);

            case 28: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , , , , , , , , , , , , , , , , , , , , , , , , , ,>).MakeGenericType(args), true) as IGetter);

            case 29: return(ReflectionAdapter.CreateInstance(typeof(GetterCore <, , , , , , , , , , , , , , , , , , , , , , , , , , , ,>).MakeGenericType(args), true) as IGetter);
            }
            throw new NotSupportedException("The maximum number of methods arguments that can be used is 30.");
        }
示例#7
0
        static object GetMemberObject(MemberExpression exp)
        {
            var    names = new List <string>();
            object targt = null;
            Type   type  = null;

            //find member root object.
            var member = exp;

            while (member != null)
            {
                names.Add(member.Member.Name);

                //static method.
                if (member.Expression == null)
                {
                    type = member.Member.DeclaringType;
                    break;
                }

                var constant = member.Expression as ConstantExpression;
                if (constant != null)
                {
                    targt = constant.Value;
                    type  = constant.Type;
                    break;
                }

                var method = member.Expression as MethodCallExpression;
                if (method != null)
                {
                    type  = method.Type;
                    targt = GetMethodObject(method);
                    break;
                }

                var newExp = member.Expression as NewExpression;
                if (newExp != null)
                {
                    type  = newExp.Type;
                    targt = GetNewObject(newExp);
                    break;
                }

                member = member.Expression as MemberExpression;
            }

            //name.
            var getterName = type.FullName + "@" + string.Join("@", names.ToArray());

            //getter.
            IGetter getter;

            lock (_memberGet)
            {
                if (!_memberGet.TryGetValue(getterName, out getter))
                {
                    var        param  = Expression.Parameter(type, "param");
                    Expression target = param;
                    names.Reverse();
                    if (targt == null)
                    {
                        target = ReflectionAdapter.StaticPropertyOrField(type, names[0]);
                        names.RemoveAt(0);
                    }
                    if (names.Count == 0)
                    {
                        getter = ReflectionAdapter.CreateInstance(typeof(GetterCore), true) as IGetter;
                        getter.Init(Expression.Convert(target, typeof(object)), new ParameterExpression[0]);
                    }
                    else
                    {
                        foreach (var e in names)
                        {
                            target = Expression.PropertyOrField(target, e);
                        }
                        getter = ReflectionAdapter.CreateInstance(typeof(GetterCore <>).MakeGenericType(type), true) as IGetter;
                        getter.Init(Expression.Convert(target, typeof(object)), new[] { param });
                    }
                    _memberGet.Add(getterName, getter);
                }
            }
            return(getter.GetMemberObject(new object[] { targt }));
        }