Detect if the type of an object is a type defined by .NET Framework

前端 未结 4 1121
我寻月下人不归
我寻月下人不归 2020-12-29 10:38

How can I determine by reflection if the type of an object is defined by a class in my own assembly or by the .NET Framework?

I dont want to supply the name of my ow

4条回答
  •  独厮守ぢ
    2020-12-29 10:44

    Based on Jon's answer and Mehrdad's comment, it appears that the following three values are used for the public key token (from AssemblyName.FullName) for the .NET Framework provided assemblies from .NET 2.0 and later:

    PublicKeyToken=b77a5c561934e089

    • mscorlib
    • System.Data
    • System.Data.OracleClient
    • System.Data.SqlXml
    • System
    • System.Runtime.Remoting
    • System.Transactions
    • System.Windows.Forms
    • System.Xml
    • SMDiagnostics
    • System.Runtime.Serialization
    • System.ServiceModel
    • System.ServiceModel.Install
    • System.ServiceModel.WasHosting

    PublicKeyToken=b03f5f7f11d50a3a

    • Accessibility
    • AspNetMMCExt
    • cscompmgd
    • CustomMarshalers
    • IEExecRemote
    • IEHost
    • IIEHost
    • ISymWrapper
    • Microsoft.Build.Conversion
    • Microsoft.Build.Engine
    • Microsoft.Build.Framework
    • Microsoft.Build.Tasks
    • Microsoft.Build.Utilities
    • Microsoft.JScript
    • Microsoft.VisualBasic.Compatibility.Data
    • Microsoft.VisualBasic.Compatibility
    • Microsoft.VisualBasic
    • Microsoft.VisualBasic.Vsa
    • Microsoft.VisualC
    • Microsoft.Vsa
    • Microsoft.Vsa.Vb.CodeDOMProcessor
    • Microsoft_VsaVb
    • sysglobl
    • System.Configuration
    • System.Configuration.Install
    • System.Deployment
    • System.Design
    • System.DirectoryServices
    • System.DirectoryServices.Protocols
    • System.Drawing.Design
    • System.Drawing
    • System.EnterpriseServices
    • System.Management
    • System.Messaging
    • System.Runtime.Serialization.Formatters.Soap
    • System.Security
    • System.ServiceProcess
    • System.Web
    • System.Web.Mobile
    • System.Web.RegularExpressions
    • System.Web.Services
    • Microsoft.Transactions.Bridge
    • Microsoft.Transactions.Bridge.Dtc
    • Microsoft.Build.Tasks.v3.5
    • Microsoft.CompactFramework.Build.Tasks
    • Microsoft.Data.Entity.Build.Tasks
    • Microsoft.VisualC.STLCLR
    • Sentinel.v3.5Client

    PublicKeyToken=31bf3856ad364e35

    • PresentationCFFRasterizer
    • PresentationUI

    This was generated from the following code:

        private void PrintAssemblyInfo(string fullName)
        {
            string[] parts = fullName.Split(',');
            Console.WriteLine(" - {0}, {1}", parts[0], parts[3]);
        }
    
        private void GenerateInfo(string path)
        {
            foreach (var file in Directory.GetFiles(path, 
               "*.dll",
               SearchOption.AllDirectories))
            {
                try
                {
                    Assembly assembly = Assembly.ReflectionOnlyLoadFrom(file);
                    PrintAssemblyInfo(assembly.GetName().FullName);
                }
                catch { }
            }
        }
    
        private void GenerateInfo()
        {
            GenerateInfo(@"C:\Windows\Microsoft.NET\Framework\v2.0.50727");
            GenerateInfo(@"C:\Windows\Microsoft.NET\Framework\v3.0");
            GenerateInfo(@"C:\Windows\Microsoft.NET\Framework\v3.5");
        }
    

提交回复
热议问题