reflect practice

ExpandedBlockStart.gif 代码

using  System;
using  System.Reflection;
using  System.Text;
using  System.Runtime.Remoting;
using  System.Globalization;

public   interface  WriteClass
{
    
void  write( string  string1);
}
public   class  InterfaceTest : WriteClass
{
    
#region  WriteClass Members

    
public   void  write( string  string1)
    {
        Console.WriteLine(
" InterfaceTest implement write: "   +  string1);
    }

    
#endregion
}
public   class  SomeTypeTest : WriteClass
{

    
private   string  _fieldString  =   string .Empty;

    
private  SomeTypeTest()
    {
    }

    
private  SomeTypeTest( string  test)
    {
        _fieldString 
=  test;
    }

    
public   string  PropertyString
    {
        
get ;
        
set ;
    }

    
public   string  ReadonlyProperty
    {
        
get  {  return  _fieldString; }
    }

    
public   static   void  DoSomethingWithStatic()
    {
        Console.WriteLine(
" Public method DoSomethingWithStatic  " );
    }
    
public   void  DoSomethingWithPublic( string  test)
    {
        Console.WriteLine(
" Public method DoSomethingWithPublic write: "   +  test);
    }

    
private   void  DoSomethingWithPrivate( string  test)
    {
        Console.WriteLine(
" private method DoSomethingWithPrivate write: "   +  test);
    }

    
public   void  DosomethingWithField()
    {
        Console.WriteLine(
" DosomethingWithField write: "   +  _fieldString);
    }

    
#region  WriteClass Members

    
public   void  write( string  string1)
    {
        Console.WriteLine(
" SomeTypeTest implement interface: "   +  string1);
    }

    
#endregion
}


public   class  TestReflection
{
    
delegate   void  TestDelegate( string  value);
    
static   void  Main()
    {
        
// create instance of SomeTypeTest class
        ConstructorInfo ci  =   typeof (SomeTypeTest).GetConstructor(BindingFlags.NonPublic  |  BindingFlags.Instance,  null , System.Type.EmptyTypes,  null );
        
object  doSomeInstance  =  ci.Invoke(System.Type.EmptyTypes);
        MethodInfo DoSomethingWithPublic 
=   typeof (SomeTypeTest).GetMethod( " DoSomethingWithPublic " );
        MethodInfo DoSomethingWithPrivate 
=   typeof (SomeTypeTest).GetMethod( " DoSomethingWithPrivate " , BindingFlags.NonPublic  |  BindingFlags.Instance);
        
//  Call an instance method defined by the SomeType type using this object.
        DoSomethingWithPublic.Invoke(doSomeInstance,  new   object [] {  " public hello "  });
        DoSomethingWithPrivate.Invoke(doSomeInstance, 
new   object [] {  " private hello "  });

        ConstructorInfo ciWithParameter 
=   typeof (SomeTypeTest).GetConstructor(BindingFlags.NonPublic  |  BindingFlags.Instance,  null new  Type[] {  typeof ( string ) },  null );
        
object  doSomeInstanceWithParamter  =  ciWithParameter.Invoke( new   object [] {  " test "  });

        MethodInfo DosomethingWithField 
=   typeof (SomeTypeTest).GetMethod( " DosomethingWithField " );
        DosomethingWithField.Invoke(doSomeInstanceWithParamter, System.Type.EmptyTypes);

        DosomethingWithField 
=   typeof (SomeTypeTest).GetMethod( " DoSomethingWithStatic " );
        DosomethingWithField.Invoke(
null null );

        PropertyInfo property 
=   typeof (SomeTypeTest).GetProperty( " PropertyString " );
        property.SetValue(doSomeInstance, 
" testProperty " null );
        Console.WriteLine(property.GetValue(doSomeInstance, 
null ));

        PropertyInfo property2 
=   typeof (SomeTypeTest).GetProperty( " ReadonlyProperty " );
        Console.WriteLine(property.GetValue(doSomeInstanceWithParamter, 
null ));

        FieldInfo field 
=   typeof (SomeTypeTest).GetField( " _fieldString " , BindingFlags.Instance  |  BindingFlags.NonPublic);
        Console.WriteLine(field.GetValue(doSomeInstanceWithParamter));

        
object  objectHandle  =  Activator.CreateInstance( typeof (SomeTypeTest), BindingFlags.NonPublic  |  BindingFlags.Public  |  BindingFlags.Instance,  null new   object [] {  " test "  }, CultureInfo.CurrentCulture);
        SomeTypeTest st 
=  (SomeTypeTest)objectHandle;
        st.DosomethingWithField();
        Console.WriteLine(st.ReadonlyProperty);

        
// test delegate
        TestDelegate method  =  (TestDelegate)Delegate.CreateDelegate( typeof (TestDelegate), doSomeInstance,  " DoSomethingWithPublic " );
        method(
" Test Delegate " );

        WriteClass writeClass 
=  (WriteClass)Activator.CreateInstance( typeof (InterfaceTest), BindingFlags.NonPublic  |  BindingFlags.Public  |  BindingFlags.Instance,  null null , CultureInfo.CurrentCulture);
        writeClass.write(
" WriteClass " );
        st.write(
" WriteClass " );
        Console.ReadLine();
    }
}


 

转载于:https://www.cnblogs.com/JessieDong/archive/2010/11/01/1866392.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值