コード例 #1
0
        public static Type ArreglaId(this Type type)
        {
            const string ID = "Id";
            Type         tipoGen;
            TypeBuilder  builder = type.GetTypeBuilder();
            IEnumerable <PropiedadTipo> propiedades = type.GetPropiedadesTipo();

            if (!propiedades.Any(p => p.Nombre == ID || p.Nombre == p.Tipo.Name + ID))
            {
                builder.AddProperty(ID, typeof(int));
            }
            foreach (PropiedadTipo propiedad in propiedades.Where(p => p.Tipo.IsTypeValidOne()))
            {
                if (!propiedades.Any(p => p.Nombre == propiedad.Nombre + ID))
                {
                    if (propiedad.Atributos.Any(a => a.Equals(typeof(RequiredAttribute))))
                    {
                        builder.AddProperty(propiedad.Nombre + ID, typeof(int));
                    }
                    else
                    {
                        builder.AddProperty(propiedad.Nombre + ID, typeof(int?));
                    }
                }
            }
            tipoGen = builder.CreateType();
            return(tipoGen.GetPropiedadesTipo().Count() == propiedades.Count() ? type : tipoGen);
        }
コード例 #2
0
        private void BuildSignatureOutput(TypeBuilder outputBuilder)
        {
            if (!Properties[PropertyNames.ReadSignature].GetValue <bool>())
            {
                return;
            }

            var signatureBuilder = new TypeBuilder();

            signatureBuilder.AddProperty(OutputNames.SignedBy, typeof(string));
            signatureBuilder.AddProperty(OutputNames.SignedAt, typeof(string));
            signatureBuilder.AddProperty(OutputNames.Reason, typeof(string));
            signatureBuilder.AddProperty(OutputNames.SignedOn, typeof(DateTime));
            signatureBuilder.AddProperty(OutputNames.Unmodified, typeof(bool));
            signatureBuilder.AddProperty(OutputNames.SignedRevision, typeof(int));
            signatureBuilder.AddProperty(OutputNames.IsLatestRevision, typeof(bool));
            signatureBuilder.AddProperty(OutputNames.Verified, typeof(bool));
            signatureBuilder.AddProperty(OutputNames.VerificationMessage, typeof(string));

            var signaturesBuilder = new TypeBuilder();

            signaturesBuilder.AddProperty(OutputNames.IsSigned, typeof(bool));
            signaturesBuilder.AddProperty(OutputNames.LatestSignature, signatureBuilder);
            signaturesBuilder.AddProperty(OutputNames.AllSignatures, signatureBuilder, true);

            outputBuilder.AddProperty(OutputNames.Signatures, signaturesBuilder);
        }
コード例 #3
0
        private ITypeReference BuildExecutionPathOutput()
        {
            bool isText = Properties[FileOpenShared.IsTextPropertyName].GetValue <bool>();

            var typeBuilder = new TypeBuilder();

            typeBuilder.AddProperty(FileOpenShared.OutputFilePathPropertyName, typeof(string), AccessType.Read);
            typeBuilder.AddProperty(FileOpenShared.OutputFileHandlePropertyName, TypeReference.CreateResource(isText ? typeof(TextFileHandle) : typeof(BinaryFileHandle)), AccessType.Read);
            return(typeBuilder.CreateTypeReference());
        }
コード例 #4
0
 private static void AddProperties(TypeBuilder typeBuilder, IEnumerable <PropertySpecification> properties)
 {
     foreach (var propertySpecification in properties)
     {
         typeBuilder.AddProperty(propertySpecification);
     }
 }
コード例 #5
0
        private void BuildSplitOutput()
        {
            var outputBuilder = new TypeBuilder();

            bool loopResultsValue = Properties[PropertyNames.SplitLoopResults].GetValue <bool>();

            if (loopResultsValue)
            {
                ExecutionPaths.Add(ExecutionPathNames.PageFiles, ExecutionPathNames.PageFiles, TypeReference.Create(typeof(string)));
            }
            else
            {
                outputBuilder.AddProperty(OutputNames.PageFiles, TypeReference.CreateList(typeof(string)));
            }

            outputBuilder.AddProperty(OutputNames.NumberOfPages, typeof(int));
            Output = outputBuilder.CreateTypeReference();
        }
コード例 #6
0
        public static Type GenType(this Type tipo, [NotNull] IEnumerable <PropiedadTipo> propiedades)
        {
            TypeBuilder builder = tipo.GetTypeBuilder();

            foreach (PropiedadTipo propiedad in propiedades)
            {
                builder.AddProperty(propiedad.Nombre, propiedad.Tipo);
            }
            return(builder.CreateType());
        }
コード例 #7
0
        public static PropertyBuilder AddProperty(this TypeBuilder Builder, PropertyInfo Property)
        {
            var property = Builder.AddProperty(Property.Name, Property.MemberType());

            foreach (var attribute in Property.GetCustomAttributesData())
            {
                property.AddAttribute(attribute);
            }

            return(property);
        }
コード例 #8
0
        private void BuildTextOutput(TypeBuilder outputBuilder)
        {
            if (!Properties[PropertyNames.ReadText].GetValue <bool>())
            {
                return;
            }

            switch (Properties[PropertyNames.SplitText].GetValue <TextSplit>())
            {
            case TextSplit.Never:
                outputBuilder.AddProperty(OutputNames.Text, typeof(string));
                break;

            case TextSplit.Page:
                outputBuilder.AddProperty(OutputNames.Text, TypeReference.CreateList(typeof(string)));
                break;

            default:
                throw new NotSupportedException("Invalid TextSplit specified.");
            }
        }
コード例 #9
0
        public TypeReference CreateTypeReference()
        {
            TypeBuilder typeBuilder = new TypeBuilder();

            foreach (Afield field in FieldList)
            {
                if (!field.Skip)
                {
                    typeBuilder.AddProperty(field.Name, field.Type.TypeReference);
                }
            }
            return(typeBuilder.CreateTypeReference());
        }
コード例 #10
0
 private void BuildExecutionPaths()
 {
     if (ReadType == FileReadOptions.LineByLine)
     {
         TypeBuilder typeBuilder = new TypeBuilder();
         typeBuilder.AddProperty(TextFileReadShared.LineNumberName, typeof(int));
         typeBuilder.AddProperty(TextFileReadShared.LineContentsName, Fields.FieldList.Any() ? Fields.CreateTypeReference() : TypeReference.Create(typeof(string)));
         GeneratedTypeReference outputType = typeBuilder.CreateTypeReference();
         if (ExecutionPaths.Contains(TextFileReadShared.ExecutionPathName))
         {
             ExecutionPaths[TextFileReadShared.ExecutionPathName].Output = outputType;
         }
         else
         {
             ExecutionPaths.Add(TextFileReadShared.ExecutionPathName, TextFileReadShared.ExecutionPathName, outputType, IterationHint.ZeroOrMore);
         }
     }
     else
     {
         ExecutionPaths.Clear();
     }
 }
コード例 #11
0
        /// <summary>
        /// Implement interface properteis
        /// </summary>
        /// <param name="typeBuilder"></param>
        /// <param name="interfaceType"></param>
        public static void ImplementInterfaceProperties(this TypeBuilder typeBuilder, Type interfaceType)
        {
            // checkc if interface type
            if (!interfaceType.IsInterface)
            {
                throw new NotSupportedException();
            }

            // add required properties
            interfaceType.GetProperties().ToList().ForEach(p => {
                typeBuilder.AddProperty(p.PropertyType, p.Name);
            });
        }
コード例 #12
0
        public static Type GetIdEntity <TProperty>(string strTableName)
        {
            TypeBuilder typeBuilder = DynamicTypeHelper.BuildType("IdEntity", "Entitys", "SyncJob.Domain");

            //定义构造器参数
            Type[]   ctorParams      = new Type[] { typeof(string) };
            object[] ctorParamValues = new object[] { $"`#Temp`{strTableName}" };
            typeBuilder.AddAttribute <TableAttribute>(ctorParams, ctorParamValues);
            var id = typeBuilder.AddProperty <TProperty>("Id");

            typeBuilder.AddCtor(new Type[] { typeof(TProperty) }, new FieldBuilder[] { id });
            //return typeBuilder.CreateType();
            return(null);
        }
コード例 #13
0
        private static ITypeReference BuildResultSetTypeReference(DatabaseModel.ResultSet resultSet)
        {
            if (resultSet.CustomType != null)
            {
                return(resultSet.CustomType);
            }

            TypeBuilder typeBuilder = new TypeBuilder();

            foreach (var nextField in resultSet.Fields)
            {
                typeBuilder.AddProperty(nextField.OutputName, nextField.DataType.GetSystemType().MapType());
            }
            return(typeBuilder.CreateTypeReference());
        }
コード例 #14
0
        private void BuildFormDataOutput(TypeBuilder outputBuilder)
        {
            if (!Properties[PropertyNames.ReadFormData].GetValue <bool>())
            {
                return;
            }

            switch (Properties[PropertyNames.ReturnFormDataAs].GetValue <FormExtraction>())
            {
            case FormExtraction.CustomType:
                ITypeReference dataType = Properties[PropertyNames.FormDataType].GetValue <ITypeReference>() ?? TypeReference.Create(typeof(object));
                outputBuilder.AddProperty(OutputNames.FormData, dataType);
                break;

            case FormExtraction.Infer:
                var fieldsBuilder  = new TypeBuilder();
                var sampleFilePath = Properties[PropertyNames.SamplePdf].GetValue <string>();
                try
                {
                    foreach (string fieldName in GetFieldNames(sampleFilePath))
                    {
                        fieldsBuilder.AddProperty(Names.GetValidName(fieldName), typeof(string));
                    }
                }
                catch { }
                outputBuilder.AddProperty(OutputNames.FormData, fieldsBuilder.CreateTypeReference());
                break;

            case FormExtraction.List:
                outputBuilder.AddProperty(OutputNames.FormDataList, TypeReference.CreateList(typeof(KeyValuePair <string, string>)));
                break;

            default:
                throw new NotSupportedException("Invalid FormExtraction specified.");
            }
        }
コード例 #15
0
        private void BuildOutput()
        {
            var outParameters = Parameters.Where(p => (p.Direction != DatabaseModel.ParameterDirection.In) && (p.DataType != DatabaseModel.DataType.RefCursor));

            if ((outParameters.Any()) || ((OutputOption != OutputOption.RowByRow) && (ResultSetCount > 0)))
            {
                TypeBuilder typeBuilder = new TypeBuilder();

                if (outParameters.Any())
                {
                    TypeBuilder outParametersTypeBuilder = new TypeBuilder();
                    foreach (var nextParameter in outParameters)
                    {
                        outParametersTypeBuilder.AddProperty(nextParameter.OutputPropertyName, nextParameter.DataType.GetSystemType().MapType());
                    }
                    typeBuilder.AddProperty(ExecuteStoredProcedureShared.OutParametersOutputPropertyName, outParametersTypeBuilder);
                }

                if (OutputOption != OutputOption.RowByRow)
                {
                    for (int i = 0; i < ResultSetCount; i++)
                    {
                        ITypeReference rowTypeReference            = BuildResultSetTypeReference(ResultSetCollection[i]);
                        string         resultSetOutputPropertyName = string.Format(OutputOption == OutputOption.ListOfRows ? ExecuteStoredProcedureShared.ResultSetRowsOutputPropertyNameFormat : ExecuteStoredProcedureShared.ResultSetRowOutputPropertyNameFormat,
                                                                                   ResultSetCount == 1 ? string.Empty : (i + 1).ToString());
                        typeBuilder.AddProperty(resultSetOutputPropertyName, OutputOption == OutputOption.ListOfRows ? TypeReference.CreateList(rowTypeReference) : rowTypeReference);
                    }
                }

                Output = typeBuilder.CreateTypeReference();
            }
            else
            {
                Output = null;
            }
        }
コード例 #16
0
        public static List <PropertyBuilder> ImplementInterface(this TypeBuilder typeBuilder, Type Interface)
        {
            var retList = new List <PropertyBuilder>();
            var methodAttributesForInterface = InheritedFromInterfacePropertyAttributes;

            var members = Interface.GetProperties();

            typeBuilder.AddInterfaceImplementation(Interface);

            foreach (var member in members)
            {
                retList.Add(typeBuilder.AddProperty(member.Name, member.MemberType(), methodAttributesForInterface));
            }

            return(retList);
        }
コード例 #17
0
        public ITypeReference BuildRowTypeFromFields()
        {
            if (Fields.Count == 0)
            {
                return(null);
            }

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

            TypeBuilder typeBuilder = new TypeBuilder();

            foreach (ResultTypeField nextField in Fields)
            {
                typeBuilder.AddProperty(nextField.Name, nextField.TypeReference);
            }
            return(typeBuilder.CreateTypeReference());
        }
コード例 #18
0
        private void RefreshExecutionPath()
        {
            var columns = Properties[DbBulkCopyShared.ColumnsPropertyName].GetValue <DatabaseModel.Columns>();

            if (columns.Any())
            {
                TypeReference outputTypeReference = new DynamicCompiledTypeReference(typeof(Loader), _ =>
                {
                    var rowTypeBuilder = new TypeBuilder();
                    foreach (var nextColumn in columns)
                    {
                        Type systemType             = nextColumn.DataType.GetSystemType();
                        bool isNullableType         = systemType.IsClass;
                        ITypeReference propertyType = isNullableType ? systemType.MapType() : TypeReference.Create(typeof(Nullable <>).MakeGenericType(systemType));

                        rowTypeBuilder.AddProperty(
                            DbBulkCopyShared.GetPropertyName(nextColumn.Name),
                            propertyType);
                    }
                    return(rowTypeBuilder.CreateTypeReference());
                });

                if (ExecutionPaths.Any())
                {
                    ExecutionPaths[DbBulkCopyShared.ExecutionPathName].Output = outputTypeReference;
                }
                else
                {
                    ExecutionPaths.Add(DbBulkCopyShared.ExecutionPathName, DbBulkCopyShared.ExecutionPathName, outputTypeReference, IterationHint.Once);
                }
            }
            else if (ExecutionPaths.Any())
            {
                ExecutionPaths.Clear();
            }
        }
コード例 #19
0
 public static PropertyBuilder AddProperty <T>(this TypeBuilder Builder, string MemberName, MethodAttributes MAttributes = DefaultPropertyAttributes)
 => Builder.AddProperty(MemberName, typeof(T), MAttributes);
コード例 #20
0
 internal void AddProperty(Builder.PropertyInfo property)
 {
     _typeBuilder.AddProperty(property);
 }
コード例 #21
0
        /// <summary>
        /// Create proxy type of specified type.
        /// </summary>
        /// <param name="moduleBuilder">Instance of <see cref="System.Reflection.Emit.ModuleBuilder"/>.</param>
        /// <param name="type">Type of interface to be implemented by the proxy type.</param>
        /// <returns>Generated proxy type that implements the specified interface.</returns>
        public static Type CreateProxyTypeOf(this ModuleBuilder moduleBuilder, Type type)
        {
            if (moduleBuilder is null)
            {
                throw new ArgumentNullException(nameof(moduleBuilder));
            }

            if (type is null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            if (!type.IsInterface)
            {
                throw new InvalidOperationException();
            }

            TypeBuilder typeBuilder = moduleBuilder.GetProxyTypeBuilder(type);

            typeBuilder.AddDefaultConstructor();

            var interfaces = type.GetInterfaces();

            // Properties are added as real getters and setters,
            // methods are added as dummy methods.
            var propertyInfos = type.GetProperties().Union(interfaces.SelectMany(i => i.GetProperties())).ToArray();

            var properties = propertyInfos.ToDictionary(p => p.Name, p => p.PropertyType);

            // Filter methods which names starts with get_ or set_ to not add as methods because they are added as properties.
            MethodInfo[] methods     = type.GetMethods().Union(interfaces.SelectMany(i => i.GetMethods())).ToArray();
            var          methodInfos = new List <MethodInfo>(methods.Length);

            foreach (var methodInfo in methods)
            {
                if (methodInfo.Name.StartsWith("get_", StringComparison.InvariantCulture) && methodInfo.GetParameters().Length == 0)
                {
                    // This method is property getter.
                    properties.TryAdd(methodInfo.Name.Substring("get_".Length), methodInfo.ReturnType);
                }
                else if (methodInfo.Name.StartsWith("set_", StringComparison.InvariantCulture) && methodInfo.GetParameters().Length == 1)
                {
                    // This method is property setter.
                    properties.TryAdd(methodInfo.Name.Substring("set_".Length), methodInfo.ReturnType);
                }
                else
                {
                    // This does not look like property.
                    methodInfos.Add(methodInfo);
                }
            }

            methodInfos.TrimExcess();

            // Add properties.
            foreach (var property in properties)
            {
                typeBuilder.AddProperty(property.Key, property.Value, methods);
            }

            // Add methods.
            foreach (var methodInfo in methodInfos)
            {
                typeBuilder.AddDummyMethod(methodInfo);
            }

            return(typeBuilder.CreateType());
        }
コード例 #22
0
 public static void AddProperty(this TypeBuilder typeBuilder, string propName, Type propertyType)
 {
     typeBuilder.AddProperty(propName, PropertyAttributes.None, propertyType);
 }
コード例 #23
0
 public static void AddProperty(this TypeBuilder typeBuilder, string propName, PropertyInfo propertyInfo)
 {
     typeBuilder.AddProperty(propName, propertyInfo.Attributes, propertyInfo.PropertyType);
 }
コード例 #24
0
ファイル: ContractsBuilder.cs プロジェクト: cime/CoreSharp
        private void AddProperty(TypeBuilder typeBuilder, PropertyInfo prop, List <Type> recursionBag, ref int order)
        {
            // build in type - no changes, just map
            if (ContractsConstants.BuildInTypes.Contains(prop.PropertyType))
            {
                var propBuilder = typeBuilder.AddProperty(prop.PropertyType, prop.Name, true, true);
                propBuilder.AddAttribute(typeof(DataMemberAttribute), null, new Dictionary <string, object> {
                    { "Order", order }
                });
                return;
            }

            // not mappable
            if (!IsMappableType(prop.PropertyType))
            {
                order = order - 1;
                return;
            }

            // replace type
            if (GetReplaceType(prop.PropertyType) != null)
            {
                var replaceType = GetReplaceType(prop.PropertyType);
                var propBuilder = typeBuilder.AddProperty(replaceType, prop.Name, true, true);
                propBuilder.AddAttribute(typeof(DataMemberAttribute), null, new Dictionary <string, object> {
                    { "Order", order }
                });
                return;
            }

            // nullable types - use internal type
            if (prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                var recordType = prop.PropertyType.GetGenericArguments().First();
                if (!IsMappableType(recordType))
                {
                    order = order - 1;
                    return;
                }
                var recordChType = recordType;
                if (IsComplexType(recordType))
                {
                    AddType(recordType, recursionBag);
                    recordChType = _dic[recordType];
                }
                else if (GetReplaceType(recordType) != null)
                {
                    recordChType = GetReplaceType(recordType);
                }
                var propBuilder = typeBuilder.AddProperty(typeof(ProtoNullable <>).MakeGenericType(recordChType), prop.Name, true, true);
                propBuilder.AddAttribute(typeof(DataMemberAttribute), null, new Dictionary <string, object> {
                    { "Order", order }
                });
                return;
            }

            // enumerables interfaces
            var enumerableInterface = GetEnumerableInterface(prop.PropertyType);
            var dictionaryInterface = GetDictionaryInterface(prop.PropertyType);

            // list like enumerable only
            if (enumerableInterface != null && dictionaryInterface == null)
            {
                var recordType = enumerableInterface.GetGenericArguments().First();
                if (!IsMappableType(recordType))
                {
                    order = order - 1;
                    return;
                }

                var name         = recordType.Name;
                var recordChType = recordType;
                if (IsComplexType(recordType))
                {
                    AddType(recordType, recursionBag);
                    recordChType = _dic[recordType];
                }
                else if (GetReplaceType(recordType) != null)
                {
                    recordChType = GetReplaceType(recordType);
                }
                var propType    = typeof(IEnumerable <>).MakeGenericType(recordChType);
                var propBuilder = typeBuilder.AddProperty(propType, prop.Name, true, true);
                propBuilder.AddAttribute(typeof(DataMemberAttribute), null, new Dictionary <string, object> {
                    { "Order", order }
                });
                return;
            }

            // obj
            if (!prop.PropertyType.Namespace.StartsWith(nameof(System)))
            {
                AddType(prop.PropertyType, recursionBag);
                var propBuilder = typeBuilder.AddProperty(_dic[prop.PropertyType], prop.Name, true, true);
                propBuilder.AddAttribute(typeof(DataMemberAttribute), null, new Dictionary <string, object> {
                    { "Order", order }
                });
                return;
            }

            // dictionary
            if (dictionaryInterface != null)
            {
                // todo: implement
            }

            // no mapping found - revert counter
            order = order - 1;
        }