Use reflection to modify private members outside the class

Source: Internet
Author: User
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Reflection;using System.ComponentModel;namespace AttributeTest{    public class Program    {             static void Main(string[] args)        {          AttributeTest aT=new AttributeTest();          aT.FunAttributeTest();          FunAttributeTest(aT);          Console.ReadLine();        }        public static void FunAttributeTest(AttributeTest aT)        {            FieldInfo[] objFields = aT.GetType().GetFields(AttributeTest.bindingFlags);            foreach (FieldInfo fi in objFields)            {                // Get the custom attributes                if (fi.FieldType == typeof(string))                {                    fi.SetValue(aT, fi.GetValue(aT)+" and I'm modified out of my class.");                    Console.WriteLine("" + fi.GetValue(aT));                }                Attribute[] attributes = System.Attribute.GetCustomAttributes(fi);                foreach (Attribute attribute in attributes)                {                    if (attribute is DescriptionAttribute)                    {                        DescriptionAttribute attributeT = attribute as DescriptionAttribute;                        Console.WriteLine("" + attributeT.Description);                    }                }            }        }    }
public  class AttributeTest    {        public static BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;        private string mPrivate = "I'm a private member.";        public string mPublic = "I'm a private member.";        [Description("Publisher of a given software inventory record")]        private string publisher = "publisher";        [Description("Title of a given software inventory record")]        private string title = "title";        [System.Xml.Serialization.XmlAttributeAttribute()]        public string Publisher        {           get            {                return this.publisher;            }            set            {                this.publisher = value;            }        }        public string Title        {            get            {                return this.title;            }            set            {                this.title = value;            }        }                public void FunAttributeTest()        {            FieldInfo[] objFields = this.GetType().GetFields(bindingFlags);            foreach (FieldInfo fi in objFields)            {                // Get the custom attributes                if (fi.FieldType==typeof(string))                {                    Console.WriteLine(""+fi.GetValue(this));                }                Attribute[] attributes = System.Attribute.GetCustomAttributes(fi);                foreach (Attribute attribute in attributes)                {                    if (attribute is DescriptionAttribute)                    {                        DescriptionAttribute attributeT = attribute as DescriptionAttribute;                        Console.WriteLine(""+attributeT.Description);                    }                }            }        }    }}

 

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.