private void LoadTypeDescriptionByType(Type type, ComInterfaceDescription comDescription, Assembly assembly)
        {
            VariableType    classType       = GetKindOfType(type);
            TypeDescription typeDescription = new TypeDescription()
            {
                AssemblyName = assembly.GetName().Name,
                Category     = LibraryCategory.Platform.ToString(),
                Description  = "",
                Name         = GetTypeName(type),
                Namespace    = GetNamespace(type),
                Kind         = classType
            };

            ClassInterfaceDescription classDescription = new ClassInterfaceDescription()
            {
                ClassTypeDescription = typeDescription,
                IsStatic             = false,
                Name = typeDescription.Name,
                Kind = classType
            };

            AddConstructorDescription(type, classDescription, classType);
            AddMethodDescription(type, classDescription);

            comDescription.Classes.Add(classDescription);
            comDescription.TypeDescriptions.Add(typeDescription);
        }
示例#2
0
        public IClassInterfaceDescription GetClassDescriptionByType(ITypeData typeData, out IAssemblyInfo assemblyInfo)
        {
            string assemblyName = typeData.AssemblyName;
            ComInterfaceDescription    interfaceDescription = _descriptionData.GetComDescription(assemblyName);
            IClassInterfaceDescription classDescription     = null;

            // 如果该类型描述已存在则直接返回
            if (interfaceDescription != null &&
                null != (classDescription = interfaceDescription.Classes.FirstOrDefault(item => item.ClassType.Equals(typeData))))
            {
                assemblyInfo = interfaceDescription.Assembly;
                return(classDescription);
            }
            string path, version;

            classDescription = _loaderManager.GetClassDescription(typeData, _descriptionData, out path, out version);

            assemblyInfo = GetAssemblyInfo(assemblyName);
            TestflowRunner testflowRunner;

            if (null == assemblyInfo && null != (testflowRunner = TestflowRunner.GetInstance()))
            {
                assemblyInfo              = testflowRunner.SequenceManager.CreateAssemblyInfo();
                assemblyInfo.Path         = path;
                assemblyInfo.Version      = version;
                assemblyInfo.AssemblyName = assemblyName;
                assemblyInfo.Available    = true;
                _descriptionData.Add(assemblyInfo);
            }
            return(classDescription);
        }
        private void AddDataTypeDescription(ComInterfaceDescription descriptionData, Type classType, string assemblyName, VariableType classKind)
        {
            TestflowCategoryAttribute category        = classType.GetCustomAttribute <TestflowCategoryAttribute>();
            DescriptionAttribute      descriptionAttr = classType.GetCustomAttribute <DescriptionAttribute>();

            string typeCategory        = null != category ? category.CategoryString : string.Empty;
            string classDescriptionStr = (null != descriptionAttr) ? descriptionAttr.Description : string.Empty;

            TypeDescription typeDescription = new TypeDescription()
            {
                AssemblyName = assemblyName,
                Category     = typeCategory,
                Description  = classDescriptionStr,
                Name         = GetTypeName(classType),
                Namespace    = GetNamespace(classType),
                Kind         = classKind
            };

            // 枚举类型需要添加枚举值到类型信息中
            if (classType.IsEnum)
            {
                typeDescription.Enumerations = Enum.GetNames(classType);
            }

            descriptionData.TypeDescriptions.Add(typeDescription);
        }
示例#4
0
        public ComInterfaceDescription LoadAssemblyDescription(string path, DescriptionDataTable descriptionCollection)
        {
            IAssemblyInfo assemblyInfo = _sequenceManager.CreateAssemblyInfo();

            assemblyInfo.Path = path;
            string assemblyName = string.Empty;
            string version      = string.Empty;
            ComInterfaceDescription assemblyDescription = _loader.LoadAssemblyDescription(path, ref assemblyName, ref version);

            CheckAssemblyDescription(assemblyDescription, assemblyInfo.AssemblyName, path);

            assemblyInfo.AssemblyName    = assemblyName;
            assemblyInfo.Version         = version;
            assemblyDescription.Assembly = assemblyInfo;

            ModuleUtils.ValidateComDescription(_sequenceManager, assemblyDescription, descriptionCollection);
            assemblyInfo.Available = true;
            descriptionCollection.Add(assemblyDescription);
            // 如果一个AppDomain载入过多的程序集,则卸载该AppDomain,构造新的AppDomain
//            if (_assemblyCount > Constants.MaxAssemblyCount)
//            {
//                AppDomain.Unload(_loaderDomain);
//                Thread.MemoryBarrier();
//                _loaderDomain = AppDomain.CreateDomain(Constants.AppDomainName);
//                _loader = (AssemblyDescriptionLoader)_loaderDomain.CreateInstanceAndUnwrap(_assemblyFullName, _loaderName);
//                Interlocked.Exchange(ref _assemblyCount, 0);
//            }
            return(assemblyDescription);
        }
示例#5
0
        public ComInterfaceDescription GetComDescriptionByPath(string path)
        {
            _lock.EnterReadLock();
            ComInterfaceDescription description =
                _descriptions.Values.FirstOrDefault(item => item.Assembly.Path.Equals(path));

            _lock.ExitReadLock();
            return(description);
        }
示例#6
0
        public ComInterfaceDescription GetComDescription(int componentId)
        {
            _lock.EnterReadLock();
            ComInterfaceDescription description =
                _descriptions.Values.FirstOrDefault(item => item.ComponentId == componentId);

            _lock.ExitReadLock();
            return(description);
        }
示例#7
0
        public IComInterfaceDescription GetComponentInterface(string path)
        {
            ComInterfaceDescription description = _descriptionData.GetComDescriptionByPath(path);

            if (null == description)
            {
                description = _loaderManager.LoadAssemblyDescription(path, _descriptionData);
            }
            return(description);
        }
示例#8
0
        public IComInterfaceDescription GetComponentInterface(IAssemblyInfo assemblyInfo)
        {
            ComInterfaceDescription description = _descriptionData.GetComDescription(assemblyInfo.AssemblyName);

            if (null == description)
            {
                description = _loaderManager.LoadAssemblyDescription(assemblyInfo, _descriptionData);
            }
            return(description);
        }
示例#9
0
        // 使用TypeDescription信息更新VariableTypes和Class中的ClassType信息
        public static void ValidateComDescription(ISequenceManager sequenceManager, ComInterfaceDescription description,
                                                  DescriptionDataTable descriptionCollection)
        {
            int componentId = description.ComponentId;

            foreach (ITypeDescription typeDescription in description.TypeDescriptions)
            {
                ITypeData classType = GetTypeDataByDescription(sequenceManager, descriptionCollection, typeDescription);
                description.VariableTypes.Add(classType);
                if (null != typeDescription.Enumerations)
                {
                    description.Enumerations.Add(GetFullName(typeDescription), typeDescription.Enumerations);
                }
            }
            description.TypeDescriptions.Clear();
            description.TypeDescriptions = null;
            ((List <ITypeData>)description.VariableTypes).TrimExcess();

            foreach (ClassInterfaceDescription classDescription in description.Classes)
            {
                ITypeData classType = GetTypeDataByDescription(sequenceManager, descriptionCollection,
                                                               classDescription.ClassTypeDescription);
                classDescription.ClassType            = classType;
                classDescription.ClassTypeDescription = null;
            }
            ((List <IClassInterfaceDescription>)description.Classes).TrimExcess();

            I18N i18N = I18N.GetInstance(Constants.I18nName);

            foreach (IClassInterfaceDescription classDescription in description.Classes)
            {
                foreach (IFuncInterfaceDescription functionDescription in classDescription.Functions)
                {
                    // 配置实例属性配置方法和静态属性配置方法的描述信息
                    if (functionDescription.FuncType == FunctionType.InstancePropertySetter)
                    {
                        functionDescription.Description = i18N.GetStr("InstancePropertySetter");
                    }
                    else if (functionDescription.FuncType == FunctionType.StaticPropertySetter)
                    {
                        functionDescription.Description = i18N.GetStr("StaticPropertySetter");
                    }
                    foreach (IArgumentDescription argumentDescription in functionDescription.Arguments)
                    {
                        InitializeArgumentType(sequenceManager, descriptionCollection, argumentDescription);
                    }
                    ((List <IArgumentDescription>)functionDescription.Arguments).TrimExcess();
                    functionDescription.ClassType = classDescription.ClassType;
                    if (null != functionDescription.Return)
                    {
                        InitializeArgumentType(sequenceManager, descriptionCollection, functionDescription.Return);
                    }
                }
            }
        }
示例#10
0
 public static void SetComponentId(ComInterfaceDescription comDescription, int index)
 {
     comDescription.ComponentId = index;
     foreach (IClassInterfaceDescription classDescription in comDescription.Classes)
     {
         classDescription.ComponentIndex = index;
         foreach (IFuncInterfaceDescription funcDescription in classDescription.Functions)
         {
             funcDescription.ComponentIndex = index;
         }
     }
 }
示例#11
0
        public ComInterfaceDescription GetComDescription(string assemblyName)
        {
            ComInterfaceDescription description = null;

            _lock.EnterReadLock();

            if (_descriptions.ContainsKey(assemblyName))
            {
                description = _descriptions[assemblyName];
            }
            _lock.ExitReadLock();
            return(description);
        }
示例#12
0
        public string[] GetEnumItems(ITypeData typeData)
        {
            ComInterfaceDescription interfaceDescription = _descriptionData.GetComDescription(typeData.Name);
            string fullName = ModuleUtils.GetFullName(typeData);

            if (null != interfaceDescription)
            {
                return(interfaceDescription.Enumerations.ContainsKey(fullName)
                    ? interfaceDescription.Enumerations[fullName]
                    : new string[0]);
            }
            return(_loaderManager.GetEnumItemsByType(typeData));
        }
示例#13
0
        public ComInterfaceDescription Remove(string assemblyName)
        {
            ComInterfaceDescription description = null;

            _lock.EnterWriteLock();
            if (_descriptions.ContainsKey(assemblyName))
            {
                description = _descriptions[assemblyName];
                _descriptions.Remove(assemblyName);
            }
            _lock.ExitWriteLock();
            return(description);
        }
示例#14
0
        private void LoadMscorLibDescription(DescriptionDataTable descriptionCollection)
        {
            Assembly assembly = typeof(int).Assembly;
            ComInterfaceDescription mscoreLibDescription = _loader.LoadMscorlibDescription();

            IAssemblyInfo assemblyInfo = _sequenceManager.CreateAssemblyInfo();

            assemblyInfo.Path         = assembly.Location;
            assemblyInfo.AssemblyName = assembly.GetName().Name;

            assemblyInfo.Version = assembly.GetName().Version.ToString();

            CheckAssemblyDescription(mscoreLibDescription, assemblyInfo.AssemblyName, assemblyInfo.Path);
            ModuleUtils.ValidateComDescription(_sequenceManager, mscoreLibDescription, descriptionCollection);
            assemblyInfo.Available        = true;
            mscoreLibDescription.Assembly = assemblyInfo;
            descriptionCollection.Add(mscoreLibDescription);
        }
        private void AddClassDescription(ComInterfaceDescription comDescription, Type classType, string assemblyName, VariableType classKind)
        {
            TestflowTypeAttribute     testflowType    = classType.GetCustomAttribute <TestflowTypeAttribute>();
            TestflowCategoryAttribute category        = classType.GetCustomAttribute <TestflowCategoryAttribute>();
            DescriptionAttribute      descriptionAttr = classType.GetCustomAttribute <DescriptionAttribute>();
            string typeCategory = null != category ? category.CategoryString : string.Empty;

            string classDescriptionStr = (null != descriptionAttr) ? descriptionAttr.Description : string.Empty;

            TypeDescription typeDescription = new TypeDescription()
            {
                AssemblyName = assemblyName,
                Category     = typeCategory,
                Name         = GetTypeName(classType),
                Namespace    = GetNamespace(classType),
                Description  = classDescriptionStr,
                Kind         = classKind
            };
            ClassInterfaceDescription classDescription = new ClassInterfaceDescription()
            {
                ClassTypeDescription = typeDescription,
                Description          = typeDescription.Description,
                Name = GetTypeName(classType),
                Kind = classKind
            };

            AddConstructorDescription(classType, classDescription, classKind);
            AddPropertySetterDescription(classType, classDescription);
            AddFieldSetterDescription(classType, classDescription);
            AddMethodDescription(classType, classDescription);

            classDescription.IsStatic = classKind == VariableType.Class && classDescription.Functions.All(
                item => item.FuncType != FunctionType.Constructor && item.FuncType != FunctionType.InstanceFunction);

            comDescription.Classes.Add(classDescription);

            // 如果是Testflow数据类型,则添加到数据类型列表中。默认所有的实例类都是Testflow数据类型(包含实例方法,并且有公开的实例属性)
            if (!classDescription.IsStatic && (null == testflowType || testflowType.IsTestflowDataType))
            {
                comDescription.TypeDescriptions.Add(typeDescription);
            }
        }
示例#16
0
        public bool Add(ComInterfaceDescription description)
        {
            bool addSuccess = false;

            _lock.EnterWriteLock();
            if (!_descriptions.ContainsKey(description.Assembly.AssemblyName))
            {
                _descriptions.Add(description.Assembly.AssemblyName, description);
                addSuccess = true;
            }
            if (_refAssemblyInfos.ContainsKey(description.Assembly.AssemblyName))
            {
                _refAssemblyInfos.Remove(description.Assembly.AssemblyName);
            }
            _lock.ExitWriteLock();

            ModuleUtils.SetComponentId(description, NextComId);

            return(addSuccess);
        }
示例#17
0
 public void RemoveAssembly(string assemblyName)
 {
     _lock.EnterWriteLock();
     if (_descriptions.ContainsKey(assemblyName))
     {
         ComInterfaceDescription description = _descriptions[assemblyName];
         _descriptions.Remove(assemblyName);
         foreach (ITypeData variableType in description.VariableTypes)
         {
             _typeMapping.Remove(ModuleUtils.GetFullName(variableType));
         }
         foreach (IClassInterfaceDescription classDescription in description.Classes)
         {
             _typeMapping.Remove(ModuleUtils.GetFullName(classDescription.ClassType));
         }
         if (!_refAssemblyInfos.ContainsKey(assemblyName))
         {
             _refAssemblyInfos.Add(assemblyName, description.Assembly);
         }
     }
     _lock.ExitWriteLock();
 }
        public ComInterfaceDescription LoadMscorlibDescription()
        {
            Assembly mscorAssembly = typeof(int).Assembly;

            _assemblies.Add(mscorAssembly.GetName().Name, mscorAssembly);
            ComInterfaceDescription description = new ComInterfaceDescription()
            {
                Category    = LibraryCategory.Platform.ToString(),
                Description = "mscorelib",
                Name        = mscorAssembly.GetName().Name,
                Signature   = mscorAssembly.GetName().Name,
            };
            List <Type> loadTypes = new List <Type>()
            {
                typeof(object),
                typeof(bool),
                typeof(double),
                typeof(float),
                typeof(long),
                typeof(ulong),
                typeof(int),
                typeof(uint),
                typeof(short),
                typeof(ushort),
                typeof(char),
                typeof(byte),
                typeof(string),
                typeof(Array),
                typeof(IntPtr),
                typeof(UIntPtr)
            };

            foreach (Type loadType in loadTypes)
            {
                LoadTypeDescriptionByType(loadType, description, mscorAssembly);
            }
            return(description);
        }
示例#19
0
        private void CheckAssemblyDescription(ComInterfaceDescription description, string assemblyName, string path)
        {
            if (null != description && _loader.Exception == null && _loader.ErrorCode == 0)
            {
                return;
            }
            I18N        i18N       = I18N.GetInstance(Constants.I18nName);
            ILogService logService = TestflowRunner.GetInstance().LogService;
            string      assembly   = assemblyName;

            if (string.IsNullOrWhiteSpace(assembly))
            {
                assembly = path;
            }
            switch (_loader.ErrorCode)
            {
            case ModuleErrorCode.HighVersion:
                logService.Print(LogLevel.Warn, CommonConst.PlatformLogSession,
                                 $"The version of assembly '{assembly}' is higher than version defined in data.");
                break;

            case ModuleErrorCode.LowVersion:
                logService.Print(LogLevel.Error, CommonConst.PlatformLogSession,
                                 $"The version of assembly '{assembly}' is lower than version defined in data.");
                throw new TestflowRuntimeException(ModuleErrorCode.LowVersion,
                                                   i18N.GetFStr("LowAssemblyVersion", assembly));
                break;

            case ModuleErrorCode.LibraryLoadError:
                if (null != _loader.Exception)
                {
                    logService.Print(LogLevel.Error, CommonConst.PlatformLogSession, _loader.Exception,
                                     $"Assembly '{assembly}' load error.");
                    throw new TestflowRuntimeException(ModuleErrorCode.LibraryLoadError,
                                                       i18N.GetFStr(_loader.Exception.Message), _loader.Exception);
                }
                else
                {
                    logService.Print(LogLevel.Error, CommonConst.PlatformLogSession,
                                     $"Assembly '{assembly}' load error.");
                    throw new TestflowRuntimeException(ModuleErrorCode.LibraryLoadError,
                                                       i18N.GetStr("RuntimeError"));
                }

            case ModuleErrorCode.LibraryNotFound:
                throw new TestflowRuntimeException(ModuleErrorCode.LibraryNotFound,
                                                   i18N.GetFStr("LibNotFound", assembly));

            default:
                if (null != _loader.Exception)
                {
                    throw new TestflowRuntimeException(ModuleErrorCode.LibraryLoadError,
                                                       i18N.GetFStr("RuntimeError", _loader.Exception.Message), _loader.Exception);
                }
                if (null == description)
                {
                    throw new TestflowRuntimeException(ModuleErrorCode.LibraryLoadError,
                                                       i18N.GetStr("RuntimeError"));
                }
                break;
            }
        }
        public ComInterfaceDescription LoadAssemblyDescription(string path, ref string assemblyName, ref string version)
        {
            Exception = null;
            ErrorCode = 0;
            Assembly assembly;

            if (!_assemblies.ContainsKey(assemblyName))
            {
                if (!File.Exists(path))
                {
                    this.ErrorCode = ModuleErrorCode.LibraryNotFound;
                    return(null);
                }
                try
                {
                    assembly = Assembly.LoadFrom(path);
                }
                catch (Exception ex)
                {
                    this.Exception = ex;
                    this.ErrorCode = ModuleErrorCode.LibraryLoadError;
                    return(null);
                }
                if (string.IsNullOrWhiteSpace(assemblyName))
                {
                    assemblyName = assembly.GetName().Name;
                    version      = assembly.GetName().Version.ToString();
                }
                else
                {
                    if (!CheckVersion(version, assembly))
                    {
                        return(null);
                    }
                }
                if (!_assemblies.ContainsKey(assemblyName))
                {
                    _assemblies.Add(assemblyName, assembly);
                }
            }
            else
            {
                assembly = _assemblies[assemblyName];
            }

            try
            {
                ComInterfaceDescription descriptionData = new ComInterfaceDescription()
                {
                    Category  = string.Empty,
                    Signature = assembly.FullName,
                    Name      = assembly.GetName().Name
                };
//                descriptionData.Assembly = assemblyInfo;
                // TODO 加载xml文件注释
                foreach (Type typeInfo in assembly.GetExportedTypes())
                {
                    HideAttribute hideAttribute = typeInfo.GetCustomAttribute <HideAttribute>();

                    //如果隐藏属性为true,则隐藏该类型。如果是屏蔽类型,则跳过
                    if ((null != hideAttribute && hideAttribute.Hide) || _ignoreClass.Any(item => typeInfo.Name.EndsWith(item)))
                    {
                        continue;
                    }
                    VariableType classKind = GetKindOfType(typeInfo);
                    if (classKind == VariableType.Enumeration || classKind == VariableType.Value)
                    {
                        AddDataTypeDescription(descriptionData, typeInfo, assemblyName, classKind);
                    }
                    else if (classKind == VariableType.Class || classKind == VariableType.Struct)
                    {
                        AddClassDescription(descriptionData, typeInfo, assemblyName, classKind);
                    }
                }
                return(descriptionData);
            }
            catch (Exception ex)
            {
                Exception      = ex;
                this.ErrorCode = ModuleErrorCode.LibraryLoadError;
                return(null);
            }
        }