Csharp/C Sharp by API/System.Reflection/MemberInfo
Содержание
MemberInfo.DeclaringType
using System;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
class MainClass {
static void Main() {
Type t = typeof(double);
Console.WriteLine("Type Name: " + t.Name);
Console.WriteLine("Full Name: " + t.FullName);
Console.WriteLine("Namespace: " + t.Namespace);
Type tBase = t.BaseType;
if (tBase != null)
Console.WriteLine("Base Type:" + tBase.Name);
Type tUnderlyingSystem = t.UnderlyingSystemType;
if (tUnderlyingSystem != null)
Console.WriteLine("UnderlyingSystem Type:" + tUnderlyingSystem.Name);
Console.WriteLine("\nPUBLIC MEMBERS:");
MemberInfo[] Members = t.GetMembers();
foreach (MemberInfo NextMember in Members) {
Console.WriteLine(NextMember.DeclaringType + " " + NextMember.MemberType + " " + NextMember.Name);
}
}
}
MemberInfo.GetCustomAttributes
using System;
using System.Diagnostics;
using System.Reflection;
[AttributeUsage(AttributeTargets.Class)]
public class ClassAuthorAttribute : Attribute
{
private string AuthorName;
public ClassAuthorAttribute(string AuthorName)
{
this.AuthorName = AuthorName;
}
public string Author
{
get
{
return AuthorName;
}
}
}
[ClassAuthor("AA")]
public class TestClass
{
public void Method1()
{
Console.WriteLine("Hello from Method1!");
}
[Conditional("DEBUG")]
public void Method2()
{
Console.WriteLine("Hello from Method2!");
}
public void Method3()
{
Console.WriteLine("Hello from Method3!");
}
}
public class MainClass
{
public static void Main()
{
TestClass MyTestClass = new TestClass();
MyTestClass.Method1();
MyTestClass.Method2();
MyTestClass.Method3();
object [] ClassAttributes;
MemberInfo TypeInformation;
TypeInformation = typeof(TestClass);
ClassAttributes = TypeInformation.GetCustomAttributes(typeof(ClassAuthorAttribute), false);
if(ClassAttributes.GetLength(0) != 0)
{
ClassAuthorAttribute ClassAttribute;
ClassAttribute = (ClassAuthorAttribute)(ClassAttributes[0]);
Console.WriteLine("Class Author: {0}", ClassAttribute.Author);
}
}
}
MemberInfo.MemberType
using System;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
class MainClass {
static void Main() {
Type t = typeof(double);
Console.WriteLine("Type Name: " + t.Name);
Console.WriteLine("Full Name: " + t.FullName);
Console.WriteLine("Namespace: " + t.Namespace);
Type tBase = t.BaseType;
if (tBase != null)
Console.WriteLine("Base Type:" + tBase.Name);
Type tUnderlyingSystem = t.UnderlyingSystemType;
if (tUnderlyingSystem != null)
Console.WriteLine("UnderlyingSystem Type:" + tUnderlyingSystem.Name);
Console.WriteLine("\nPUBLIC MEMBERS:");
MemberInfo[] Members = t.GetMembers();
foreach (MemberInfo NextMember in Members) {
Console.WriteLine(NextMember.DeclaringType + " " + NextMember.MemberType + " " + NextMember.Name);
}
}
}
MemberInfo.Name
using System;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
class MainClass {
static void Main() {
Type t = typeof(double);
Console.WriteLine("Type Name: " + t.Name);
Console.WriteLine("Full Name: " + t.FullName);
Console.WriteLine("Namespace: " + t.Namespace);
Type tBase = t.BaseType;
if (tBase != null)
Console.WriteLine("Base Type:" + tBase.Name);
Type tUnderlyingSystem = t.UnderlyingSystemType;
if (tUnderlyingSystem != null)
Console.WriteLine("UnderlyingSystem Type:" + tUnderlyingSystem.Name);
Console.WriteLine("\nPUBLIC MEMBERS:");
MemberInfo[] Members = t.GetMembers();
foreach (MemberInfo NextMember in Members) {
Console.WriteLine(NextMember.DeclaringType + " " + NextMember.MemberType + " " + NextMember.Name);
}
}
}