Prüft, ob ein Typ ein Teil des .NET Frameworks ist oder nicht.
using System;
using System.Collections.Generic;
public static class DotNetTools
{
/// <summary>
/// Checks if a type is part of the Microsoft .NET framework
/// </summary>
/// <param name="type">Type</param>
/// <returns>Is part of the .NET framework (true) or not (false)</returns>
public static bool IsDotNetType(Type type)
{
if (null == type)
{
throw new ArgumentNullException("type");
}
// prepare list of known .NET-Assemblies
List<Assembly> asms = new List<Assembly>();
// System
asms.Add(Assembly.GetAssembly(typeof(object)));
// System.Data
asms.Add(Assembly.GetAssembly(typeof(System.Data.IDbConnection)));
// System.Drawing
asms.Add(Assembly.GetAssembly(typeof(System.Drawing.Size)));
// System.Text.RegularExpressions
asms.Add(Assembly.GetAssembly(typeof(System.Text.RegularExpressions.Regex)));
// System.Web
asms.Add(Assembly.GetAssembly(typeof(System.Web.HttpUtility)));
// System.Windows.Forms
asms.Add(Assembly.GetAssembly(typeof(System.Windows.Forms.Control)));
// System.Xml
asms.Add(Assembly.GetAssembly(typeof(System.Xml.XmlNode)));
// System.Xml.Linq (.NET 3.0)
asms.Add(Assembly.GetAssembly(typeof(System.Xml.Linq.XObject)));
bool isDotNet = false;
// search list
foreach (Assembly asm in asms)
{
if (asm.GetType(type.FullName, false, false) is Type)
{
// Names are OK => check if same assemblies
isDotNet = asm.Equals(type.Assembly);
if (isDotNet)
{
break;
}
// Assemblies are not the same
}
}
return isDotNet;
}
}
1 Kommentare zum Snippet