[英]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 我已经测试了一些方法
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)程序集将具有公共密钥令牌b77a5c561934e089
或b03f5f7f11d50a3a
。 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.