About the encapsulation of. NET Reflection

Source: Internet
Author: User
Tags object net reflection reflector return
. NET reflection provides a way to get the object type metadata at run time so that the program can dynamically invoke the object's properties and methods. The cost of dynamic is that the reflection invocation is not as concise as a direct call based on static type, and lacks the type checking mechanism, and it loses the IDE's smart hints and is prone to error; so many friends try to. NET reflection for encapsulation.

This topic is the beholder, benevolent, here I also talk about myself. NET Reflection package, please look at the following sample code:
static void Main (string[] args)
{
Person Liu = new person ("Liu", 26);
Reflector Reflector = new Reflector (Liu);
Get Properties
String name = Reflector. Property<string> ("Name");
int age = Reflector. Property<int> ("Age");
Console.WriteLine (name + "" + age);
modifying properties
Age = Reflector. Setproperty<int> ("Age", 27);
Console.WriteLine (name + "" + age);
Get process
proc<string> SayHello = Reflector. Proc<string> ("SayHello");
SayHello ("Ling");
Get function
func<int> getage = Reflector. Func<int> ("Getage");
Age = Getage ();
Console.WriteLine (age);
Console.ReadLine ();
}
public class Person
{
private string name;
private int age;
Public person (string name, int age)
{
THIS.name = name;
This.age = age;
}
public string Name
{
get {return name;}
}
public int Age
{
get {return age;}
set {age = value;}
}
public void SayHello (string who)
{
Console.WriteLine ("Say Hello to" + Who);
}
public int Getage ()
{
return age;
}
}
I believe you have seen from the code the idea of encapsulation: using generics and generic delegates to add static type constraints to dynamic reflection. Here's a quick look at the key parts of the reflector implementation:
public delegate void Proc ();
public delegate void Proc<t1> (T1 arg1);
public delegate void Proc<t1, t2> (T1 arg1, T2 args);
public delegate void Proc<t1, T2, t3> (T1 arg1, T2 args, T3 arg3);
public delegate void Proc<t1, T2, T3, t4> (T1 arg1, T2 args, T3 arg3, T4 arg4);
public delegate void Proc<t1, T2, T3, T4, t5> (T1 arg1, T2 args, T3 arg3, T4 arg4, T5 arg5);
Public delegate R func<r> ();
Public delegate R Func<t1, r> (T1 arg1);
Public delegate R Func<t1, T2, r> (T1 arg1, T2 args);
Public delegate R Func<t1, T2, T3, r> (T1 arg1, T2 args, T3 arg3);
Public delegate R Func<t1, T2, T3, T4, r> (T1 arg1, T2 args, T3 arg3, T4 arg4);
Public delegate R Func<t1, T2, T3, T4, T5, r> (T1 arg1, T2 args, T3 arg3, T4 arg4, T5 arg5);
public class Reflector
{
Private object target;
public Object Target
{
get {return target;}
}
Public T property<t> (string name)
{
PropertyInfo pi = target. GetType (). GetProperty (name, typeof (T));
if (null!= pi && pi. CanRead)
{
Object value = Pi. GetValue (target, NULL);
if (null!= value)
{
Return (T) value;
}
}
return default (T);
}
Public T Setproperty<t> (string name, T value)
{
PropertyInfo pi = target. GetType (). GetProperty (name, typeof (T));
if (null!= pi && pi. CanWrite)
{
Pi. SetValue (target, value, NULL);
}
return value;
}
Public Proc Proc (string name)
{
MethodInfo mi = target. GetType (). GetMethod (name, type.emptytypes);
if (null!= mi)
{
Return Delegate.createdelegate (typeof Proc), Target, MI. Name, false) as Proc;
}
return null;
}
Public proc<t> proc<t> (string name)
{
MethodInfo mi = target. GetType (). GetMethod (name, new type[] {typeof (T)});
if (null!= mi)
{
Return Delegate.createdelegate (typeof proc<t>), Target, MI. Name, false) as proc<t>;
}
return null;
}
Public Proc<t1, T2> proc<t1, t2> (string name)
{
MethodInfo mi = target. GetType (). GetMethod (name, new type[] {typeof (T1), typeof (T2)});
if (null!= mi)
{
Return Delegate.createdelegate (typeof (Proc<t1, t2>), Target, MI. Name, False) as PROC&LT;T1, t2>;
}
return null;
}
Public Proc<t1, T2, t3> proc<t1, T2, t3> (string name)
{
//...
}
Public Proc<t1, T2, T3, t4> proc<t1, T2, T3, t4> (string name)
{
//...
}
Public Proc<t1, T2, T3, T4, t5> proc<t1, T2, T3, T4, t5> (string name)
{
//...
}
Public func<r> func<r> (string name)
{
MethodInfo mi = target. GetType (). GetMethod (name, type.emptytypes);
if (null!= mi)
{
Return Delegate.createdelegate (typeof func<r>), Target, MI. Name, false) as func<r>;
}
return null;
}
Public Func<t1, R> func<t1, r> (string name)
{
MethodInfo mi = target. GetType (). GetMethod (name, new type[] {typeof (T1)});
if (null!= mi)
{
Return Delegate.createdelegate (typeof (Func<t1, r>), Target, MI. Name, False) as FUNC&LT;T1, r>;
}
return null;
}
Public Func<t1, T2, r> func<t1, T2, r> (string name)
{
//...
}
}
The encapsulation implementation is not complex, but it uses generics and generic delegates to provide the caller with strongly typed properties and methods, except that the name of the property and method is dynamic, and the rest can be type constrained. Welcome to this topic a lot of exchanges!



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.