/// <summary>导入某类型,导入程序集引用及命名空间引用,主要处理泛型</summary> /// <param name="item"></param> /// <param name="type"></param> void ImportType(TemplateItem item, Type type) { String name = null; try { name = type.Assembly.Location; } catch { } if (!String.IsNullOrEmpty(name) && !AssemblyReferences.Contains(name)) { AssemblyReferences.Add(name); } name = type.Namespace; if (!item.Imports.Contains(name)) { item.Imports.Add(name); } if (type.IsGenericType && !type.IsGenericTypeDefinition) { Type[] ts = type.GetGenericArguments(); foreach (Type elm in ts) { if (!elm.IsGenericParameter) { ImportType(item, elm); } } } }
/// <summary> /// Removes the supplied assembly from the compilers reference collection. /// </summary> /// <param name="assembly"></param> public static void RemoveAssemblyReference(String assembly) { if (AssemblyReferences.Contains(assembly)) { AssemblyReferences.Remove(assembly); } }
/// <summary> /// Adds a reference to the supplied Assembly to the compilers reference collection. /// </summary> /// <param name="assembly"></param> public static void AddAssemblyReference(Assembly assembly) { if (!AssemblyReferences.Contains(assembly.GetName().Name)) { AssemblyReferences.Add(assembly.GetName().Name); } }
/// <summary> /// Adds a reference to the supplied Assembly name to the compilers reference collection. /// </summary> /// <param name="assembly"></param> public static void AddAssemblyReference(String assembly) { if (!AssemblyReferences.Contains(assembly)) { AssemblyReferences.Add(assembly); } }
public InteractivePackage AddAssemblyReference( FilePath assemblyReferencePath) { if (assemblyReferencePath.IsNull) { return(this); } if (AssemblyReferences.Contains(assemblyReferencePath)) { return(this); } return(new InteractivePackage( Identity, IsExplicit, AssemblyReferences.Add(assemblyReferencePath), SupportedVersionRange)); }
private TemplateItem ProcessDirective(Directive directive, TemplateItem item) { #region 包含include if (String.Equals(directive.Name, "include", StringComparison.OrdinalIgnoreCase)) { String name = directive.GetParameter("name"); // 可能采用了相对路径 if (!File.Exists(name)) { name = Path.Combine(Path.GetDirectoryName(item.Name), name); } String content = null; TemplateItem ti = FindTemplateItem(name); if (ti != null) { ti.Included = true; content = ti.Content; } else { // 尝试读取文件 if (File.Exists(name)) { ti = new TemplateItem(); ti.Name = name; ti.Content = File.ReadAllText(name); Templates.Add(ti); ti.Included = true; content = ti.Content; } } // 允许内容为空 //if (String.IsNullOrEmpty(content)) throw new TemplateException(directive.Block, String.Format("加载模版[{0}]失败!", name)); return(ti); } #endregion if (String.Equals(directive.Name, "assembly", StringComparison.OrdinalIgnoreCase)) { String name = directive.GetParameter("name"); if (!AssemblyReferences.Contains(name)) { AssemblyReferences.Add(name); } } else if (String.Equals(directive.Name, "import", StringComparison.OrdinalIgnoreCase)) { item.Imports.Add(directive.GetParameter("namespace")); } else if (String.Equals(directive.Name, "template", StringComparison.OrdinalIgnoreCase)) { if (!item.Processed) { // 由模版指令指定类名 String name = directive.GetParameter("name"); if (!String.IsNullOrEmpty(name)) { item.ClassName = name; } //item.BaseClassName = directive.GetParameter("inherits"); if (directive.TryGetParameter("inherits", out name)) { item.BaseClassName = name; } item.Processed = true; } else { throw new TemplateException(directive.Block, "多个模版指令!"); } } else if (String.Equals(directive.Name, "var", StringComparison.OrdinalIgnoreCase)) { String name = directive.GetParameter("name"); String type = directive.GetParameter("type"); if (item.Vars.ContainsKey(name)) { throw new TemplateException(directive.Block, "模版变量" + name + "已存在!"); } Type ptype = TypeX.GetType(type, true); if (ptype == null) { throw new TemplateException(directive.Block, "无法找到模版变量类型" + type + "!"); } // 因为TypeX.GetType的强大,模版可能没有引用程序集和命名空间,甚至type位于未装载的程序集中它也会自动装载,所以这里需要加上 ImportType(item, ptype); item.Vars.Add(name, ptype); } return(null); }