📄 reflectionclass.cs
字号:
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Mike Krüger" email="mike@icsharpcode.net"/>
// <version>$Revision: 915 $</version>
// </file>
using System;
using System.Collections;
using System.Reflection;
using System.Collections.Generic;
using ICSharpCode.Core;
namespace ICSharpCode.SharpDevelop.Dom
{
[Serializable]
public class ReflectionClass : DefaultClass
{
const BindingFlags flags = BindingFlags.Instance |
BindingFlags.Static |
BindingFlags.NonPublic |
BindingFlags.DeclaredOnly |
BindingFlags.Public;
void InitMembers(Type type)
{
foreach (Type nestedType in type.GetNestedTypes(flags)) {
if (!nestedType.IsVisible) continue;
string name = nestedType.FullName.Replace('+', '.');
InnerClasses.Add(new ReflectionClass(CompilationUnit, nestedType, name, this));
}
foreach (FieldInfo field in type.GetFields(flags)) {
if (!field.IsPublic && !field.IsFamily) continue;
if (!field.IsSpecialName) {
Fields.Add(new ReflectionField(field, this));
}
}
foreach (PropertyInfo propertyInfo in type.GetProperties(flags)) {
ReflectionProperty prop = new ReflectionProperty(propertyInfo, this);
if (prop.IsPublic || prop.IsProtected)
Properties.Add(prop);
}
foreach (ConstructorInfo constructorInfo in type.GetConstructors(flags)) {
if (!constructorInfo.IsPublic && !constructorInfo.IsFamily) continue;
Methods.Add(new ReflectionMethod(constructorInfo, this));
}
foreach (MethodInfo methodInfo in type.GetMethods(flags)) {
if (!methodInfo.IsPublic && !methodInfo.IsFamily) continue;
if (!methodInfo.IsSpecialName) {
Methods.Add(new ReflectionMethod(methodInfo, this));
}
}
foreach (EventInfo eventInfo in type.GetEvents(flags)) {
Events.Add(new ReflectionEvent(eventInfo, this));
}
}
public static bool IsDelegate(Type type)
{
return type.IsSubclassOf(typeof(Delegate)) && type != typeof(MulticastDelegate);
}
static void AddAttributes(IProjectContent pc, IList<IAttribute> list, IList<CustomAttributeData> attributes)
{
foreach (CustomAttributeData att in attributes) {
DefaultAttribute a = new DefaultAttribute(att.Constructor.DeclaringType.FullName);
foreach (CustomAttributeTypedArgument arg in att.ConstructorArguments) {
IReturnType type = ReflectionReturnType.Create(pc, null, arg.ArgumentType, false);
a.PositionalArguments.Add(new AttributeArgument(type, arg.Value));
}
foreach (CustomAttributeNamedArgument arg in att.NamedArguments) {
IReturnType type = ReflectionReturnType.Create(pc, null, arg.TypedValue.ArgumentType, false);
a.NamedArguments.Add(arg.MemberInfo.Name, new AttributeArgument(type, arg.TypedValue.Value));
}
list.Add(a);
}
}
public ReflectionClass(ICompilationUnit compilationUnit, Type type, string fullName, IClass declaringType) : base(compilationUnit, declaringType)
{
if (fullName.Length > 2 && fullName[fullName.Length - 2] == '`') {
FullyQualifiedName = fullName.Substring(0, fullName.Length - 2);
} else {
FullyQualifiedName = fullName;
}
this.UseInheritanceCache = true;
try {
AddAttributes(compilationUnit.ProjectContent, this.Attributes, CustomAttributeData.GetCustomAttributes(type));
} catch (Exception ex) {
ICSharpCode.Core.MessageService.ShowError(ex);
}
// set classtype
if (type.IsInterface) {
this.ClassType = ClassType.Interface;
} else if (type.IsEnum) {
this.ClassType = ClassType.Enum;
} else if (type.IsValueType) {
this.ClassType = ClassType.Struct;
} else if (IsDelegate(type)) {
this.ClassType = ClassType.Delegate;
} else {
this.ClassType = ClassType.Class;
foreach (IAttribute att in this.Attributes) {
if (att.Name == "Microsoft.VisualBasic.CompilerServices.StandardModuleAttribute"
|| att.Name == "Boo.Lang.ModuleAttribute")
{
this.ClassType = ClassType.Module;
break;
}
}
}
if (type.IsGenericTypeDefinition) {
foreach (Type g in type.GetGenericArguments()) {
this.TypeParameters.Add(new DefaultTypeParameter(this, g));
}
int i = 0;
foreach (Type g in type.GetGenericArguments()) {
((DefaultTypeParameter)this.TypeParameters[i++]).AddConstraintsFromType(g);
}
}
ModifierEnum modifiers = ModifierEnum.None;
if (type.IsNestedAssembly) {
modifiers |= ModifierEnum.Internal;
}
if (type.IsSealed) {
modifiers |= ModifierEnum.Sealed;
}
if (type.IsAbstract) {
modifiers |= ModifierEnum.Abstract;
}
if (type.IsNestedPrivate ) { // I assume that private is used most and public last (at least should be)
modifiers |= ModifierEnum.Private;
} else if (type.IsNestedFamily ) {
modifiers |= ModifierEnum.Protected;
} else if (type.IsNestedPublic || type.IsPublic) {
modifiers |= ModifierEnum.Public;
} else if (type.IsNotPublic) {
modifiers |= ModifierEnum.Internal;
} else if (type.IsNestedFamORAssem || type.IsNestedFamANDAssem) {
modifiers |= ModifierEnum.Protected;
modifiers |= ModifierEnum.Internal;
}
this.Modifiers = modifiers;
// set base classes
if (type.BaseType != null) { // it's null for System.Object ONLY !!!
BaseTypes.Add(ReflectionReturnType.Create(this, type.BaseType, false));
}
foreach (Type iface in type.GetInterfaces()) {
BaseTypes.Add(ReflectionReturnType.Create(this, iface, false));
}
InitMembers(type);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -