示例#1
0
        private void ValidateContractType(Type type, Type contractType)
        {
            bool isImplementInterface     = type.GetTypeInfo().GetInterfaces().Any(x => x.GetTypeInfo() == contractType.GetTypeInfo());
            bool isImplementAbstractClass = type.GetTypeInfo().BaseType.GetTypeInfo() == contractType.GetTypeInfo();

            if (!isImplementInterface && !isImplementAbstractClass)
            {
                throw new AmbiguousMatchException("Class must implemented contract type");
            }

            ExportAttribute exportAttribute = type.GetTypeInfo().GetCustomAttribute <ExportAttribute>();

            if (exportAttribute.Contract?.GetTypeInfo() != contractType.GetTypeInfo())
            {
                throw new AmbiguousMatchException($"Don't have attribute {type.GetTypeInfo().Name} with contract type {contractType.GetTypeInfo().Name}");
            }
        }
        public void AddAssembly(Assembly assembly)
        {
            var types = assembly.GetTypes()
                        .Where(type => type.IsClass);

            foreach (var type in types)
            {
                ExportAttribute exportAttribute = type.GetCustomAttributes <ExportAttribute>(inherit: false)
                                                  .FirstOrDefault(attr => attr.Contract != null);

                Type bindTo = exportAttribute == null ?
                              type :
                              exportAttribute.Contract;

                this.AddType(type, bindTo);
            }
        }