using System;
namespace AttTargsCS {
// This attribute is only valid on a class.
[AttributeUsage(AttributeTargets.Class)]
public class ClassTargetAttribute : Attribute {
}
// This attribute is only valid on a method.
[AttributeUsage(AttributeTargets.Method)]
public class MethodTargetAttribute : Attribute {
}
// This attribute is only valid on a constructor.
[AttributeUsage(AttributeTargets.Constructor)]
public class ConstructorTargetAttribute : Attribute {
}
// This attribute is only valid on a field.
[AttributeUsage(AttributeTargets.Field)]
public class FieldTargetAttribute : Attribute {
}
// This attribute is valid on a class or a method.
[AttributeUsage(AttributeTargets.Class|AttributeTargets.Method)]
public class ClassMethodTargetAttribute : Attribute {
}
// This attribute is valid on a generic type parameter.
[AttributeUsage(AttributeTargets.GenericParameter)]
public class GenericParameterTargetAttribute : Attribute {
}
// This attribute is valid on any target.
[AttributeUsage(AttributeTargets.All)]
public class AllTargetsAttribute : Attribute {
}
[ClassTarget]
[ClassMethodTarget]
[AllTargets]
public class TestClassAttribute {
[ConstructorTarget]
[AllTargets]
TestClassAttribute() {
}
[MethodTarget]
[ClassMethodTarget]
[AllTargets]
public void Method1() {
}
[FieldTarget]
[AllTargets]
public int myInt;
public void GenericMethod<
[GenericParameterTarget, AllTargets] T>(T x) {
}
static void Main(string[] args) {
}
}
}
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
class MultiUseAttr : Attribute { }
[MultiUseAttr, MultiUseAttr]
class Class2 { }
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method |
AttributeTargets.Property | AttributeTargets.Field,
Inherited = true)]
public class InheritedAttribute : Attribute
{}
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method |
AttributeTargets.Property | AttributeTargets.Field,
Inherited = false)]
public class NotInheritedAttribute : Attribute
{}
using System;
using System.Reflection;
[InheritedAttribute]
public class BaseA
{
[InheritedAttribute]
public virtual void MethodA()
{}
}
public class DerivedA : BaseA
{
public override void MethodA()
{}
}
[NotInheritedAttribute]
public class BaseB
{
[NotInheritedAttribute]
public virtual void MethodB()
{}
}
public class DerivedB : BaseB
{
public override void MethodB()
{}
}
public class Example
{
public static void Main()
{
Type typeA = typeof(DerivedA);
Console.WriteLine("DerivedA has Inherited attribute: {0}",
typeA.GetCustomAttributes(typeof(InheritedAttribute), true).Length > 0);
MethodInfo memberA = typeA.GetMethod("MethodA");
Console.WriteLine("DerivedA.MemberA has Inherited attribute: {0}\n",
memberA.GetCustomAttributes(typeof(InheritedAttribute), true).Length > 0);
Type typeB = typeof(DerivedB);
Console.WriteLine("DerivedB has Inherited attribute: {0}",
typeB.GetCustomAttributes(typeof(InheritedAttribute), true).Length > 0);
MethodInfo memberB = typeB.GetMethod("MethodB");
Console.WriteLine("DerivedB.MemberB has Inherited attribute: {0}",
memberB.GetCustomAttributes(typeof(InheritedAttribute), true).Length > 0);
}
}
// The example displays the following output:
// DerivedA has Inherited attribute: True
// DerivedA.MemberA has Inherited attribute: True
//
// DerivedB has Inherited attribute: False
// DerivedB.MemberB has Inherited attribute: False
System.Reflection.Assembly System.Reflection.MemberInfo System.Reflection.EventInfo System.Reflection.FieldInfo System.Reflection.MethodBase System.Reflection.ConstructorInfo System.Reflection.MethodInfo System.Reflection.PropertyInfo System.Type
public void Process(object processObj)
{
Type t = processsObj.GetType();
if(t.GetInterface(“ITest”) !=null)
…
}
public class TestClass {
private string _value;
public TestClass() {
}
public TestClass(string value) {
_value = value;
}
public string GetValue( string prefix ) {
if( _value==null )
return "NULL";
else
return prefix+" : "+_value;
}
//获取类型信息
Type t = Type.GetType("TestSpace.TestClass");
//构造器的参数
object[] constuctParms = new object[]{"timmy"};
//根据类型创建对象
object dObj = Activator.CreateInstance(t,constuctParms);
//获取方法的信息
MethodInfo method = t.GetMethod("GetValue");
//调用方法的一些标志位,这里的含义是Public并且是实例方法,这也是默认的值
BindingFlags flag = BindingFlags.Public | BindingFlags.Instance;
//GetValue方法的参数
object[] parameters = new object[]{"Hello"};
//调用方法,用一个object接收返回值
object returnValue = method.Invoke(dObj,flag,Type.DefaultBinder,parameters,null);
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有