예제 #1
0
        public static string GetExportDirName <T>()
        {
            var attrs = typeof(T).GetCustomAttributes(typeof(ExportAttribute), false);

            if (attrs.Length > 0)
            {
                ExportAttribute attr = (ExportAttribute)attrs[0];
                return(attr.dirName);
            }
            return(null);
        }
예제 #2
0
        public void AsContractType_SetsContractType()
        {
            var builder = new ConventionBuilder();

            builder.ForTypesDerivedFrom <IFoo>().Export((e) => e.AsContractType(typeof(IFoo)));

            ExportAttribute exportAtt = GetExportAttribute(builder);

            Assert.Equal(typeof(IFoo), exportAtt.ContractType);
            Assert.Null(exportAtt.ContractName);
        }
예제 #3
0
 private static Type GetTypeIdentityTypeFromExport(this MemberInfo member, ExportAttribute export)
 {
     if (export.ContractType != null)
     {
         return(export.ContractType.AdjustSpecifiedTypeIdentityType(member));
     }
     else
     {
         return((member.MemberType != MemberTypes.Method) ? member.GetDefaultTypeFromMember() : null);
     }
 }
예제 #4
0
        public AttributedExportDefinition(AttributedPartCreationInfo partCreationInfo, MemberInfo member, ExportAttribute exportAttribute)
            : base(member.GetContractNameFromExport(exportAttribute), (IDictionary <string, object>)null)
        {
            Assumes.NotNull(partCreationInfo);
            Assumes.NotNull(member);
            Assumes.NotNull(exportAttribute);

            this._partCreationInfo = partCreationInfo;
            this._member           = member;
            this._exportAttribute  = exportAttribute;
        }
예제 #5
0
        public void AsContractName_AndContractType_ComputeContractNameFromType()
        {
            var builder = new ConventionBuilder();

            builder.ForTypesDerivedFrom <IFoo>().Export(e => e.AsContractName(t => "Contract:" + t.FullName).AsContractType <IFoo>());

            ExportAttribute exportAtt = GetExportAttribute(builder);

            Assert.Equal("Contract:" + typeof(FooImpl).FullName, exportAtt.ContractName);
            Assert.Equal(typeof(IFoo), exportAtt.ContractType);
        }
예제 #6
0
        public void AsContractName_AndContractType_SetsContractNameAndType()
        {
            var builder = new ExportBuilder();

            builder.AsContractName("hey");
            builder.AsContractType(typeof(IFoo));

            ExportAttribute exportAtt = GetExportAttribute(builder);

            Assert.Equal("hey", exportAtt.ContractName);
            Assert.Equal(typeof(IFoo), exportAtt.ContractType);
        }
예제 #7
0
 internal static void GetContractInfoFromExport(this MemberInfo member, ExportAttribute export, out Type?typeIdentityType, out string contractName)
 {
     typeIdentityType = member.GetTypeIdentityTypeFromExport(export);
     if (!string.IsNullOrEmpty(export.ContractName))
     {
         contractName = export.ContractName;
     }
     else
     {
         contractName = member.GetTypeIdentityFromExport(typeIdentityType);
     }
 }
        public AttributedExportDefinition(AttributedPartCreationInfo partCreationInfo, MemberInfo member, ExportAttribute exportAttribute, Type typeIdentityType, string contractName)
            : base(contractName, (IDictionary <string, object>)null)
        {
            Assumes.NotNull(partCreationInfo);
            Assumes.NotNull(member);
            Assumes.NotNull(exportAttribute);

            this._partCreationInfo = partCreationInfo;
            this._member           = member;
            this._exportAttribute  = exportAttribute;
            this._typeIdentityType = typeIdentityType;
        }
예제 #9
0
        public AttributedExportDefinition(AttributedPartCreationInfo partCreationInfo, MemberInfo member, ExportAttribute exportAttribute, Type?typeIdentityType, string contractName)
            : base(contractName, (IDictionary <string, object?>?)null)
        {
            ArgumentNullException.ThrowIfNull(partCreationInfo);
            ArgumentNullException.ThrowIfNull(member);
            ArgumentNullException.ThrowIfNull(exportAttribute);

            _partCreationInfo = partCreationInfo;
            _member           = member;
            _exportAttribute  = exportAttribute;
            _typeIdentityType = typeIdentityType;
        }
예제 #10
0
        private static Type GetTypeFromContractNameAsILayoutItem(ExportAttribute attribute)
        {
            string typeName;

            if ((typeName = attribute.ContractName) == null)
            {
                return(null);
            }

            var type = Type.GetType(typeName);

            return(typeof(ILayoutItem).IsAssignableFrom(type) ? type : null);
        }
예제 #11
0
        public override void ApplyCustomAttribute(AST.SpecialAttributes kind, Attribute attribute, AST.CustomAttribute.TargetSelectors selector)
        {
            switch (kind)
            {
            case AST.SpecialAttributes.Export:
                this.exportInfo = (ExportAttribute)attribute;
                break;

            default:
                Debug.Fail("N/A");
                break;
            }
        }
        public static void ConnectMethod(MethodInfo method, Selector selector)
        {
            var type = method.DeclaringType;

            if (!Class.IsCustomType(type))
            {
                throw new ArgumentException("Cannot late bind methods on core types");
            }

            var ea    = new ExportAttribute(selector.Name);
            var klass = new Class(type);

            Class.RegisterMethod(method, ea, type, klass.Handle);
        }
예제 #13
0
        private static ExportInfo[] GetExportsFromExportAttribute(ExportAttribute attribute,
                                                                  ExportedRegistrationInfo currentInfo, Type implementationType)
        {
            var export = new ExportInfo(attribute.ContractType ?? implementationType,
                                        attribute.ContractName ??
                                        (attribute is ExportWithKeyAttribute ? ((ExportWithKeyAttribute)attribute).ContractKey : null));

            var currentExports = currentInfo.Exports;
            var exports        = currentExports == null ? new[] { export }
                : currentExports.Contains(export) ? currentExports
                : currentExports.AppendOrUpdate(export);

            return(exports);
        }
        internal static string GetTypeIdentityFromExport(this MemberInfo member, ExportAttribute export)
        {
            if (export.ContractType != null)
            {
                return(AttributedModelServices.GetTypeIdentity(export.ContractType));
            }

            if (member.MemberType == MemberTypes.Method)
            {
                return(AttributedModelServices.GetTypeIdentity((MethodInfo)member));
            }

            return(AttributedModelServices.GetTypeIdentity(member.GetDefaultTypeFromMember()));
        }
예제 #15
0
        private static void RegisterType(Type type, string lifeCycle)
        {
            var exportAttributes = type.GetCustomAttributes(typeof(ExportAttribute), false);

            if (exportAttributes == null || exportAttributes.Length == 0)
            {
                return;
            }

            ExportAttribute exportAttribute = exportAttributes[0] as ExportAttribute;

            //string[] filterArray = new string[] { "IEnumerable" };
            //Type[] objectInterfaces = interfaces.Where(pre => !filterArray.Contains(pre.Name) && !(pre.IsGenericType)).ToArray();

            //反射类型中包含的所有非系统的方法,判断方法中是否带有特性,如果参数中不包含特性,则不处理
            var allMethods = exportAttribute.InterfaceType.GetMethods(System.Reflection.BindingFlags.Public);

            if (methodMappingContainer.ContainsKey(type))
            {
                IList <ValidatorMethodMapping> methodMappings = methodMappingContainer[type];

                //添加当前方法对应的映射元数据信息
                foreach (var method in allMethods)
                {
                    ValidatorMethodMapping methodMapping = new ValidatorMethodMapping(method);

                    if (methodMapping.MethodParameters != null && methodMapping.MethodParameters.Length > 0)
                    {
                        methodMappings.Add(methodMapping);
                    }
                }
            }
            else
            {
                IList <ValidatorMethodMapping> methodMappings = new List <ValidatorMethodMapping>();

                foreach (var method in allMethods)
                {
                    ValidatorMethodMapping methodMapping = new ValidatorMethodMapping(method);

                    if (methodMapping.MethodParameters != null && methodMapping.MethodParameters.Length > 0)
                    {
                        methodMappings.Add(methodMapping);
                    }
                }

                methodMappingContainer.Add(type, methodMappings);
            }
        }
예제 #16
0
        public IEnumerable <IDependencyExportDescriptor> GetExports(Type taskType)
        {
            List <IDependencyExportDescriptor> result = new List <IDependencyExportDescriptor>();

            foreach (PropertyInfo propertyInfo in taskType.GetProperties())
            {
                ExportAttribute attribute = propertyInfo.GetCustomAttribute <ExportAttribute>();
                if (attribute != null)
                {
                    result.Add(new PropertyExportDescriptor(new ExportAttributeTarget(propertyInfo, attribute), propertyInfo));
                }
            }

            return(result);
        }
예제 #17
0
        public static IEnumerable <Type> GetExportsForType(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            List <Type> types = new List <Type>();

            foreach (Assembly extensionAssembly in assemblies)
            {
                if (extensionAssembly == null)
                {
                    continue;
                }
                Type[] publicTypes = extensionAssembly.GetExportedTypes();
                if (publicTypes == null)
                {
                    continue;
                }

                foreach (Type publicType in publicTypes)
                {
                    object[] exportAttrs = publicType.GetCustomAttributes(typeof(ExportAttribute), true);
                    if (exportAttrs == null || exportAttrs.Length < 1)
                    {
                        continue;
                    }

                    foreach (object attribObj in exportAttrs)
                    {
                        // Has an export attribute
                        ExportAttribute attrib = attribObj as ExportAttribute;
                        if (attrib == null)
                        {
                            continue;
                        }

                        if (type.Equals(attrib.ContractType))
                        {
                            types.Add(publicType);
                            break;
                        }
                    }
                }
            }
            return(types);
        }
        public void ConventionsInInnerAndOuterRCs_InnerRCTakesPrecendence()
        {
            var innerConvention = new RegistrationBuilder();

            innerConvention.ForType <ConventionTarget>().Export(eb => eb.AsContractName(ContractNames.ContractX));
            TypeInfo innerType = innerConvention.MapType(typeof(ConventionTarget).GetTypeInfo());

            var outerConvention = new RegistrationBuilder();

            outerConvention.ForType <ConventionTarget>().Export(eb => eb.AsContractName(ContractNames.ContractY));
            TypeInfo outerType = outerConvention.MapType(innerType /*.GetTypeInfo()*/);

            ExportAttribute export = outerType.GetCustomAttributes(false).OfType <ExportAttribute>().Single();

            Assert.Equal(ContractNames.ContractX, export.ContractName);
        }
예제 #19
0
        /// <summary>
        /// Ctor, make sure that T is an interface.
        ///  - Find the export for T
        ///  - Add the generic type for this instance to Compose repository
        /// </summary>
        static Compose()
        {
            if (!typeof(T).IsInterface)
            {
                throw new Exception("Repository type T can only be an interface");
            }

            ExportAttribute = typeof(T).ExportAttribute();

            if (!Compose.ExportedInterfaceFactory.ContainsKey(typeof(T)))
            {
                Compose.ExportedInterfaceFactory.Add(typeof(T), typeof(T).MakeGenericExport());
            }

            Exports = new List <Type>();
        }
        private List <ExportEntity> TransformEntityList(List <Entity> entitylist)
        {
            var entitiesToExport = new List <ExportEntity>();

            foreach (Entity e in entitylist)
            {
                var exportEntity = new ExportEntity
                {
                    Id          = e.Id,
                    LogicalName = e.LogicalName
                };

                foreach (var attribute in e.Attributes)
                {
                    // Leave out the entity id and logical name from the attribute collection .
                    if ((attribute.Key.ToUpper() != e.LogicalName.ToUpper() + "ID") &&
                        (attribute.Key.ToUpper() != "LOGICALNAME"))
                    {
                        var exportAttribute = new ExportAttribute
                        {
                            AttributeName = attribute.Key,
                            AttributeType = attribute.Value.GetType().ToString()
                        };

                        if (exportAttribute.AttributeType == "Microsoft.Xrm.Sdk.EntityCollection")
                        {
                            var ec       = (EntityCollection)attribute.Value;
                            var entities = new List <Entity>();
                            foreach (var entity in ec.Entities)
                            {
                                entities.Add(entity);
                            }
                            exportAttribute.AttributeValue = TransformEntityList(entities);
                        }
                        else
                        {
                            exportAttribute.AttributeValue = attribute.Value;
                        }
                        exportEntity.Attributes.Add(exportAttribute);
                    }
                }

                entitiesToExport.Add(exportEntity);
            }

            return(entitiesToExport);
        }
예제 #21
0
        private static List <ExportarColumnasInfo> LeerAtributos(Type tipoDTO)
        {
            //Inicializamos la lista de columnas a exportar
            var columnasExportar = new List <ExportarColumnasInfo>();

            //Iteramos entre las columnas del DTO para obtener la información de los atributos personalizados
            foreach (var infoMiembro in tipoDTO.GetMembers())
            {
                //Filtramos los campos o columnas del DTO
                if (infoMiembro.MemberType == MemberTypes.Property)
                {
                    var Campo       = (PropertyInfo)infoMiembro;
                    var NombreClase = (infoMiembro).Name;

                    if (tipoDTO.GetProperty(NombreClase) != null)
                    {
                        //Obtenemos sus atributos de tipo Export
                        ExportAttribute Atributos = (ExportAttribute)tipoDTO.GetProperty(NombreClase).GetCustomAttributes(typeof(ExportAttribute), true).SingleOrDefault();

                        //Es caso el campo o columna este decorado con el atributo creamos una linea más en las columnas a exportar
                        if (Atributos != null)
                        {
                            if (Atributos.Cabecera != null)
                            {
                                columnasExportar.Add(
                                    new ExportarColumnasInfo()
                                {
                                    Campo        = NombreClase,
                                    Propiedad    = Campo,
                                    Orden        = Atributos.Orden,
                                    Tamanio      = Atributos.Tamanio,
                                    Cabecera     = Atributos.Cabecera,
                                    Formato      = Atributos.Formato,
                                    Grupo        = Atributos.Grupo,
                                    Alineamiento = Atributos.Alineamiento
                                });
                            }
                        }
                    }
                }
            }
            return(columnasExportar);
        }
예제 #22
0
        private IEnumerable <Export> BuildExports(IEnumerable <Type> exportedTypes)
        {
            foreach (var type in exportedTypes)
            {
                ExportAttribute attr =
                    type.GetCustomAttribute <InheritedExportAttribute>(true) ??
                    type.GetCustomAttribute <ExportAttribute>(false);

                if (attr.ContractType != null && !attr.ContractType.IsAssignableFrom(type))
                {
                    this.Log.LogError(Resources.IncompatibleExportImplementation, type, attr.ContractType);
                    continue;
                }

                // Find an importing constructor, or log error.
                var export = new Export
                {
                    Contract = new Contract {
                        Type = attr.ContractType ?? type, Name = attr.ContractName
                    },
                    Implementation = type,
                    Properties     = BuildProperties(type),
                };

                if (HasNoDefaultConstructor(type))
                {
                    // Find the constructor with the proper attribute
                    var ctor = type
                               .GetConstructors()
                               .FirstOrDefault(c => c.HasCustomAttribute <ImportingConstructorAttribute>());
                    // or log an error.
                    if (ctor == null)
                    {
                        this.Log.LogError(Resources.ImportingConstructorMissing, type);
                        continue;
                    }

                    export.Parameters = BuildParameters(ctor);
                }

                yield return(export);
            }
        }
 public static void RegisterAssembly(Assembly assembly)
 {
     if (assembly == null)
     {
         return;
     }
     foreach (var obj in assembly.GetTypes().Where(t => t.GetCustomAttribute <ExportAttribute>() != null))
     {
         ExportAttribute att = obj.GetCustomAttribute <ExportAttribute>();
         if (att.ContractType != null)
         {
             _containerBuilder.RegisterType(obj).As(att.ContractType);
         }
         else
         {
             foreach (var intf in obj.GetInterfaces())
             {
                 _containerBuilder.RegisterType(obj).As(intf);
             }
         }
     }
 }
예제 #24
0
        Type GetTypeFromContractNameAsILayoutItem(ExportAttribute attribute)
        {
            if (attribute == null)
            {
                return(null);
            }

            string typeName;

            if ((typeName = attribute.ContractName) == null)
            {
                return(null);
            }

            var type = Type.GetType(typeName);

            if (type == null || !typeof(ILayoutItem).IsInstanceOfType(type))
            {
                return(null);
            }
            return(type);
        }
예제 #25
0
    void PrintFilterExport(PropertyInfo p, ExportAttribute export, bool setter)
    {
        if (export == null)
        {
            return;
        }

        var selector = export.Selector;

        if (setter)
        {
            selector = "set" + Capitalize(selector) + ":";
        }

        if (export.ArgumentSemantic != ArgumentSemantic.None && !p.PropertyType.IsPrimitive)
        {
            print($"[Export (\"{selector}\", ArgumentSemantic.{export.ArgumentSemantic})]");
        }
        else
        {
            print($"[Export (\"{selector}\")]");
        }
    }
예제 #26
0
        public AttributedExportDefinition(AttributedPartCreationInfo partCreationInfo, MemberInfo member, ExportAttribute exportAttribute, Type?typeIdentityType, string contractName)
            : base(contractName, (IDictionary <string, object?>?)null)
        {
            if (partCreationInfo == null)
            {
                throw new ArgumentNullException(nameof(partCreationInfo));
            }

            if (member == null)
            {
                throw new ArgumentNullException(nameof(member));
            }

            if (exportAttribute == null)
            {
                throw new ArgumentNullException(nameof(exportAttribute));
            }

            _partCreationInfo = partCreationInfo;
            _member           = member;
            _exportAttribute  = exportAttribute;
            _typeIdentityType = typeIdentityType;
        }
        private AttributedExportDefinition CreateExportDefinition(MemberInfo member, ExportAttribute exportAttribute)
        {
            member.GetContractInfoFromExport(exportAttribute, out Type? typeIdentityType, out string contractName);

            return(new AttributedExportDefinition(this, member, exportAttribute, typeIdentityType, contractName));
        }
예제 #28
0
        public static void TemplateCompletionHandlerExportsIVsTextViewCreationListener()
        {
            ExportAttribute attribute = typeof(TemplateCompletionHandlerProvider).GetCustomAttributes(false).OfType <ExportAttribute>().Single();

            Assert.Equal(typeof(IVsTextViewCreationListener), attribute.ContractType);
        }
예제 #29
0
 internal static bool IsContractNameSameAsTypeIdentity(this ExportAttribute export)
 {
     return(string.IsNullOrEmpty(export.ContractName));
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="ExistingEntityLookupException"/> class.
 /// </summary>
 /// <param name="ExportAttribute">The export attribute.</param>
 /// <param name="id">The identifier.</param>
 /// <param name="ex">The ex.</param>
 public ExistingEntityLookupException(ExportAttribute ExportAttribute, object id, Exception ex)
     : base(string.Format(ErrMsgFrmtStr, ExportAttribute.ContractName.ToLower(), id, ex.Message), ex)
 {
 }
예제 #31
0
        private AttributedExportDefinition CreateExportDefinition(MemberInfo member, ExportAttribute exportAttribute)
        {
            string contractName = null;
            Type typeIdentityType = null;
            member.GetContractInfoFromExport(exportAttribute, out typeIdentityType, out contractName);

            return new AttributedExportDefinition(this, member, exportAttribute, typeIdentityType, contractName);
        }