简体   繁体   English

C#类类型-如何确定它是否是标准.net框架类

[英]C# class type - How to determine whether it is a standard .net framework class

C# / .net framework C#/。net框架

What is the most reliable way to determine whether a class (type) is a class provided by the .net framework and not any of my classes or 3rd party library classes. 确定类(类型)是否是.net框架提供的类而不是我的任何类或第三方库类的最可靠方法是什么?

I have tested some approaches 我已经测试了一些方法

  • The namespace, eg starting with "System." 命名空间,例如以“ System”开头。
  • The Codebase of the assembly, where the dll is located dll所在的程序集的代码库

All this "feels" a little clumsy though it works. 所有这些“感觉”起来都有些笨拙,尽管它起作用了。

Question: What is the easiest and most reliable way to determine this? 问题:确定这一点的最简单,最可靠的方法是什么?

You could check the assembly's public key token. 您可以检查程序集的公钥令牌。 Microsoft (BCL) assemblies will have the public key token b77a5c561934e089 or b03f5f7f11d50a3a . Microsoft(BCL)程序集将具有公共密钥令牌b77a5c561934e089b03f5f7f11d50a3a WPF assemblies will have the public key token 31bf3856ad364e35 . WPF程序集将具有公共密钥令牌31bf3856ad364e35

In general, to get the public key token of an assembly, you can use sn.exe -Tp foo.dll . 通常,要获取程序集的公钥令牌,可以使用sn.exe -Tp foo.dll sn.exe is part of the Windows SDK, which you should already have. sn.exe是Windows SDK的一部分,您应该已经拥有。

You can get the public key token from the assembly's full name (eg typeof(string).Assembly.FullName ), which is just a string, or you can get the raw public key token bytes from the assembly by doing a P/Invoke into StrongNameTokenFromAssembly . 您可以从程序集的全名(例如typeof(string).Assembly.FullName )中获取公钥令牌,它只是一个字符串,也可以通过对程序集进行P / Invoke来从程序集中获取原始的公钥令牌字节。 StrongNameTokenFromAssembly

Read the Assembly Company Attribute from the assembly [assembly: AssemblyCompany("Microsoft Corporation")] 从程序集中读取Assembly Company属性[assembly:AssemblyCompany(“ Microsoft Corporation”)]

http://msdn.microsoft.com/en-us/library/y1375e30.aspx http://msdn.microsoft.com/en-us/library/y1375e30.aspx

using System;
using System.Reflection;

[assembly: AssemblyTitle("CustAttrs1CS")]
[assembly: AssemblyDescription("GetCustomAttributes() Demo")]
[assembly: AssemblyCompany("Microsoft")]

namespace CustAttrs1CS {
    class DemoClass {
        static void Main(string[] args) {
            Type clsType = typeof(DemoClass);
            // Get the Assembly type to access its metadata.
            Assembly assy = clsType.Assembly;

            // Iterate through the attributes for the assembly.
            foreach(Attribute attr in Attribute.GetCustomAttributes(assy)) {
                // Check for the AssemblyTitle attribute.
                if (attr.GetType() == typeof(AssemblyTitleAttribute))
                    Console.WriteLine("Assembly title is \"{0}\".",
                        ((AssemblyTitleAttribute)attr).Title);

                // Check for the AssemblyDescription attribute.
                else if (attr.GetType() == 
                    typeof(AssemblyDescriptionAttribute))
                    Console.WriteLine("Assembly description is \"{0}\".",
                        ((AssemblyDescriptionAttribute)attr).Description);

                // Check for the AssemblyCompany attribute.
                else if (attr.GetType() == typeof(AssemblyCompanyAttribute))
                    Console.WriteLine("Assembly company is {0}.",
                        ((AssemblyCompanyAttribute)attr).Company);
            }
        }
    }
}

A couple of ideas: 一些想法:

In Visual Studio, within Solution Explorer, expand References, right click a reference, then choose Properties and look at the path, example: 在Visual Studio的“解决方案资源管理器”中,展开“引用”,右键单击引用,然后选择“属性”并查看路径,例如:
C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727\\System.dll C:\\ WINDOWS \\ Microsoft.NET \\ Framework \\ v2.0.50727 \\ System.dll

I'm guessing that most assemblies in C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727\\ are likely to be standard .NET. 我猜想C:\\ WINDOWS \\ Microsoft.NET \\ Framework \\ v2.0.50727 \\中的大多数程序集可能都是标准的.NET。

Also, you could look up the assembly in MSDN library, example: 另外,您可以在MSDN库中查找程序集,例如:
http://msdn.microsoft.com/en-us/library/system.aspx . http://msdn.microsoft.com/zh-CN/library/system.aspx

You say about Base Class Library? 您说的是基类库吗?

You can consult here: http://en.wikipedia.org/wiki/Base_Class_Library 您可以在这里查询: http : //en.wikipedia.org/wiki/Base_Class_Library

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C# - 如何确定类型是否为数字 - C# - how to determine whether a Type is a number 如何确定给定的Type(System.Type)是否继承自特定的基类(在.Net中)? - How do you determine whether or not a given Type (System.Type) inherits from a specific base class (in .Net)? 如何确定C#中子类的类型? - How can I determine the type of a child class in C#? 如果一个类是WinRT类型,如何确定使用C#反射 - How to determine using C# Reflection if a class is a WinRT type 如何确定一个程序集是针对 .net 核心、.net 框架还是 .net 标准构建的? - How to determine whether one assembly was built against .net core, .net framework or .net standard? 如何确定运行.NET Standard类库的.NET平台? - How to determine the .NET platform that runs the .NET Standard class library? 如何确定 T 是泛型中的值类型还是引用类? - How to determine whether T is a value type or reference class in generic? c#如何判断一个变量是否为类 - how to check whether a variable is a class or not in c# 如何确定变量类型是否支持C#中的给定运算符 - How to determine whether a variable type supports given operators in C# 如何使用类库(.NET Framework)在C#中创建Excel文件? - How create file excel in C# with Class Library (.NET Framework)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM