- 积分
- 24557
- 明经币
- 个
- 注册时间
- 2004-3-17
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
楼主 |
发表于 2017-11-27 14:13:31
|
显示全部楼层
2017/11/27
修改Assembly类加载不必要类型的Bug
- using System;
- using System.Collections.Generic;
- using System.IO;
- using ComTypes = System.Runtime.InteropServices.ComTypes;
- namespace NFox.Runtime.Com.Reflection
- {
- /// <summary>
- /// Com程序集,只有类型集合,类型别名、枚举等未处理
- /// </summary>
- public class Assembly
- {
- /// <summary>
- /// 程序集字典
- /// </summary>
- private static Dictionary<AssemblyKey, Assembly> _openedAssemblies
- = new Dictionary<AssemblyKey, Assembly>();
- public static DirectoryInfo Root
- {
- get
- {
- var assem = System.Reflection.Assembly.GetExecutingAssembly();
- var fi = new FileInfo(assem.Location);
- return fi.Directory.CreateSubdirectory("ComAssemblies");
- }
- }
- /// <summary>
- /// 获取IDispatch接口对象的类型信息
- /// </summary>
- /// <param name="obj">IDispatch接口对象</param>
- /// <returns>对象的类型信息</returns>
- public static Type GetType(IDispatch obj)
- {
- AssemblyKey key;
- ComTypes.ITypeInfo info;
- obj.GetTypeInfo(0, 0, out info);
- ComTypes.ITypeLib lib;
- int id;
- info.GetContainingTypeLib(out lib, out id);
-
- IntPtr ip;
- lib.GetLibAttr(out ip);
- var la = Utils.GetObject<ComTypes.TYPELIBATTR>(ip);
- key = new AssemblyKey(la.guid, la.wMajorVerNum, la.wMinorVerNum);
- lib.ReleaseTLibAttr(ip);
- if (!_openedAssemblies.ContainsKey(key))
- {
- string name, doc, helpfile;
- int hc;
- lib.GetDocumentation(-1, out name, out doc, out hc, out helpfile);
- var dir = Root.CreateSubdirectory(name);
- var clsdir = dir.CreateSubdirectory(key.ClsId.ToString());
- var verdir = clsdir.CreateSubdirectory(key.Version);
- _openedAssemblies.Add(key, new Assembly(key, name, verdir));
- }
- return _openedAssemblies[key].GetType(id, info);
- }
- private Assembly(AssemblyKey key, string name, DirectoryInfo path)
- {
- Key = key;
- Name = name;
- _root = path;
- }
- public string Name { get; }
- public AssemblyKey Key { get; }
- private DirectoryInfo _root;
- /// <summary>
- /// 类型字典
- /// </summary>
- public Dictionary<int, Type> Types { get; }
- = new Dictionary<int, Type>();
- /// <summary>
- /// 按索引获取Com程序集中的类型信息
- /// </summary>
- /// <param name="id">索引</param>
- /// <param name="info">ITypeInfo接口</param>
- /// <returns>对应索引的类型信息</returns>
- public Type GetType(int id, ComTypes.ITypeInfo info)
- {
- if (!Types.ContainsKey(id))
- {
- var name = $"{_root.FullName}/{id}.typ";
- if (File.Exists(name))
- {
- Types.Add(id, new Type(this, name));
- }
- else
- {
- Type type = new Type(this, info);
- type.Save(name);
- Types.Add(id, type);
- }
- }
- return Types[id];
- }
- public override string ToString()
- {
- return Name;
- }
- }
- }
|
|