ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

c# – .Net 4 – 在程序集中包含自定义信息

2019-05-18 11:53:34  阅读:202  来源: 互联网

标签:c net assemblies


我正在构建一个可扩展的应用程序,它将在运行时通过Assembly.LoadFile()加载其他程序集.这些附加程序集将包含诸如WPF资源字典(皮肤等),普通资源(Resx)和/或插件类之类的东西.程序集也可以不包含公共类,只包含资源或资源字典.

我正在寻找一种识别程序集的方法,比如友好名称(如“附加外观”或“集成浏览器”),程序集的功能类型(SkinsLibrary,SkinsLibrary | PluginLibrary等)和其他信息(如ConflictsWith(new [] {“SkinsLibrary”,“BrowserPlugin”).

到目前为止,我在命名程序集中使用约定(* .Skins.* .dll等).在每个程序集中,我有一个空的虚拟类,它只是一个占位符,用于保存实际(程序集范围)信息的自定义类属性,但这感觉就像一个黑客.是否有一些简化的标准方法来处理这个问题?

我正在开发中央加载器系统,我团队中的其他开发人员将开发这些额外的程序集,所以我想最小化约定和管道细节.

解决方法:

编辑:我已经用更详细的信息更新了答案.

这是一个如何完成您想要做的事情的示例.
首先为不同类型的插件类型定义枚举.

public enum AssemblyPluginType
{
    Skins,
    Browser
}

添加两个将用于描述插件的属性(程序集插件类型和潜在冲突).

[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = false)]
public sealed class AssemblyPluginAttribute : Attribute
{
    private readonly AssemblyPluginType _type;

    public AssemblyPluginType PluginType
    {
        get { return _type; }
    }

    public AssemblyPluginAttribute(AssemblyPluginType type)
    {
        _type = type;
    }
}

[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = false)]
public sealed class AssemblyPluginConflictAttribute : Attribute
{
    private readonly AssemblyPluginType[] _conflicts;

    public AssemblyPluginType[] Conflicts
    {
        get { return _conflicts; }
    } 

    public AssemblyPluginConflictAttribute(params AssemblyPluginType[] conflicts)
    {
        _conflicts = conflicts;
    }
}

现在,您可以将这些属性添加到程序集中.

只要它们位于命名空间之外,就可以在程序集中的任何位置添加以下两行.我通常将汇编属性放在AssemblyInfo.cs文件中,该文件可以在Properties文件夹中找到.

[assembly: AssemblyPluginAttribute(AssemblyPluginType.Browser)]
[assembly: AssemblyPluginConflictAttribute(AssemblyPluginType.Skins, AssemblyPluginType.Browser)]

现在,您可以使用以下代码检查特定属性的程序集:

using System;
using System.Reflection;

namespace ConsoleApplication
{
    public class Program
    {
        public static void Main(string[] args)
        {
            // Get the assembly we're going to check for attributes.
            // You will want to load the assemblies you want to check at runtime.
            Assembly assembly = typeof(Program).Assembly;

            // Get all assembly plugin attributes that the assembly contains.
            object[] attributes = assembly.GetCustomAttributes(typeof(AssemblyPluginAttribute), false);
            if (attributes.Length == 1)
            {
                // Cast the attribute and get the assembly plugin type from it.
                var attribute = attributes[0] as AssemblyPluginAttribute;
                AssemblyPluginType pluginType = attribute.PluginType;
            }
        }
    }
}

标签:c,net,assemblies
来源: https://codeday.me/bug/20190518/1127888.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有