reflect practice

代码

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, nullnew 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(
nullnull);

        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, nullnew 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, nullnull, CultureInfo.CurrentCulture);
        writeClass.write(
"WriteClass");
        st.write(
"WriteClass");
        Console.ReadLine();
    }
}


 

posted on 2010-11-01 16:49  菡子  阅读(278)  评论(0编辑  收藏  举报