Application of C # reflection (Reflection)

Source: Internet
Author: User

Reflection (Reflection) is a very important feature in C #, and other languages have this feature, such as Java. The reflection of this feature is very practical, how practical it is, I do not know, if the use of struts, hibernate, spring and so on these frameworks, it will be aware of the characteristics of reflection is how powerful. Like I'm listing all the Java frameworks,. NET Framework I do not understand, there is no I do not know. But none of the frameworks that I have touched are those that do not use reflection, except for languages that do not have reflective properties.

Recently more tired, I will not say more, directly to see the code.

This is a class in the model assembly:

Code
using System;
using System.Collections.Generic;
using System.Text;
namespace Model
{
  public class UserInfo
  {
    private int userId;
    public int UserId
    {
      get { return userId; }
      set { userId = value; }
    }
    private string userName;
    public string UserName
    {
      get { return userName; }
      set { userName = value; }
    }
    public void Show()
    {
      Console.WriteLine("UserId:" + userId + ", UserName:" + userName);
    }
  }
}

This is the action of reflection:

Code


using System;


using System.Collections.Generic;


Using System.Text;


using System.Reflection;


namespace Objectloader


{


public class Showobject


  {


//Load Assembly


private Assembly Assembly = Assembly.Load ("Model");


///<summary>


///instantiate the class to include its namespace


///</summary>


///<param name= "objname" > class name </param>


///<returns></returns>


public Object Loadobject (string objname)


    {


return assembly. CreateInstance ("Model." + objname);


    }


///<summary>


///Returns all the public properties


///</summary>


///<param name= "obj" ></param>


///<returns></returns>


Public propertyinfo[] Getpropertys (object obj)


    {


Type type = obj. GetType ();


propertyinfo[] infos = type. GetProperties ();


return infos;


    }


///<summary>


///Set the specified property value for the instance


///</summary>


///<param name= "obj" > instance </param>


///<param name= "property" > attribute name </param>


///<param name= "value" > </param>


public void setPropertyValue (Object obj, String, object value)


    {


Type type = obj. GetType ();


PropertyInfo info = type. GetProperty (property);


if (info!= null)


      {


info. SetValue (obj, value, null);


      }


    }


<summary>


///Returns the specified property value


///</summary>


///<param name= "obj" > instance </param>


///<param name= "property" > attribute name </param>


///<returns></returns>


public Object GetPropertyValue (Object obj, string property)


    {


Type type = obj. GetType ();


PropertyInfo info = type. GetProperty (property);


if (info = null)


      {


return null;


      }


return info. GetValue (obj, null);


    }


///<summary>


///The specified method for executing an instance


///</summary>


///<param name= "obj" ></param>


///<param name= "methodname" > method name </param>


public void Executemethod (Object obj, String methodname)


    {


Type type = obj. GetType ();


MethodInfo info = type. GetMethod (methodname);


if (info!= null)


      {


info. Invoke (obj, null);


      }


    }


  }


}

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.