当前位置:  开发笔记 > 编程语言 > 正文

.NET属性列表

如何解决《.NET属性列表》经验,为你挑选了1个好方法。

是否在公共属性的某个地方有一个列表,用于像Serializable

谢谢

编辑〜我问的原因是我在ntiers ORMS中遇到了StoredProcedure属性.



1> StingyJack..:

是的,看看msdn你有没有请看这里.

编辑:此链接只回答吸.这是一个工作提取器,适用于名称中包含Attribute的所有可加载类型(gac​​).

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var process = new Process();
            //your path may vary
            process.StartInfo.FileName = @"C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools\gacutil.exe";
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.Arguments = "/l";
            process.Start();

            var consoleOutput = process.StandardOutput;


            var assemblyList = new List();
            var startAdding = false;
            while (consoleOutput.EndOfStream == false)
            {
                var line = consoleOutput.ReadLine();
                if (line.IndexOf("The Global Assembly Cache contains the following assemblies", StringComparison.OrdinalIgnoreCase) >= 0)
                {
                    startAdding = true;
                    continue;
                }

                if (startAdding == false)
                {
                    continue;
                }

                //add any other filter conditions (framework version, etc)
                if (line.IndexOf("System.", StringComparison.OrdinalIgnoreCase) < 0)
                {
                    continue;
                }

                assemblyList.Add(line.Trim());
            }

            var collectedRecords = new List();
            var failedToLoad = new List();

            Console.WriteLine($"Found {assemblyList.Count} assemblies");
            var currentItem = 1;


            foreach (var gacAssemblyInfo in assemblyList)
            {
                Console.SetCursorPosition(0, 2);
                Console.WriteLine($"On {currentItem} of {assemblyList.Count} ");
                Console.SetCursorPosition(0, 3);
                Console.WriteLine($"Loading {gacAssemblyInfo}");
                currentItem++;

                try
                {
                    var asm = Assembly.Load(gacAssemblyInfo);

                    foreach (Type t in asm.GetTypes())
                    {
                        if (t.Name.EndsWith("Attribute", StringComparison.OrdinalIgnoreCase))
                        {
                            collectedRecords.Add($"{t.FullName} - {t.Assembly.FullName}");
                        }
                    }

                }
                catch (Exception ex)
                {
                    failedToLoad.Add($"FAILED to load {gacAssemblyInfo} - {ex}");
                    Console.SetCursorPosition(1, 9);
                    Console.WriteLine($"Failure to load count: {failedToLoad.Count}");
                    Console.SetCursorPosition(4, 10);
                    Console.WriteLine($"Last Fail: {gacAssemblyInfo}");
                }
            }

            var fileBase = System.IO.Path.GetRandomFileName();
            var goodFile = $"{fileBase}_good.txt";
            var failFile = $"{fileBase}_failedToLoad.txt";
            System.IO.File.WriteAllLines(goodFile, collectedRecords);
            System.IO.File.WriteAllLines(failFile, failedToLoad);
            Console.SetCursorPosition(0, 15);
            Console.WriteLine($"Matching types: {goodFile}");
            Console.WriteLine($"Failures: {failFile}");
            Console.WriteLine("Press ENTER to exit");
            Console.ReadLine();
        }
    }
}

推荐阅读
携手相约幸福
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有