A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 周琪 中级黑马   /  2013-5-18 12:18  /  1338 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

老师,大神看下。c#中的反射可以获得父类中的私有成员么,有的话请给个例子。百度不到啊。

评分

参与人数 1技术分 +1 收起 理由
苏波 + 1

查看全部评分

2 个回复

倒序浏览
[System.AttributeUsage(System.AttributeTargets.Class |
                       System.AttributeTargets.Struct,
                       AllowMultiple = true)  // multiuse attribute
]
public class Author : System.Attribute
{
    string name;
    public double version;
    public Author(string name)
    {
        this.name = name;
        version = 1.0;  // Default value
    }
    public string GetName()
    {
        return name;
    }
}
[Author("H. Ackerman")]
private class FirstClass
{
    // ...
}
// No Author attribute
private class SecondClass
{
    // ...
}
[Author("H. Ackerman"), Author("M. Knott", version = 2.0)]
private class ThirdClass
{
    // ...
}
class TestAuthorAttribute
{
    static void Main()
    {
        PrintAuthorInfo(typeof(FirstClass));
        PrintAuthorInfo(typeof(SecondClass));
        PrintAuthorInfo(typeof(ThirdClass));
    }
    private static void PrintAuthorInfo(System.Type t)
    {
        System.Console.WriteLine("Author information for {0}", t);
        System.Attribute[] attrs = System.Attribute.GetCustomAttributes(t);  // reflection
        foreach (System.Attribute attr in attrs)
        {
            if (attr is Author)
            {
                Author a = (Author)attr;
                System.Console.WriteLine("   {0}, version {1:f}", a.GetName(), a.version);
            }
        }
    }
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
输出
Author information for FirstClass
H. Ackerman, version 1.00
Author information for SecondClass
Author information for ThirdClass
H. Ackerman, version 1.00
M. Knott, version 2.00
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
回复 使用道具 举报
实体类:

public class userinfo
    {
        public userinfo()
        { }
        #region 成员
        private int _userid;
        private string _username;
        private string _password;
        private int _roleid;
        private string _telephone;
        private string _usersex;
        private string _address;
        private string _email;
        private string _logintime;
        private string _createtime;
        #endregion

        #region 属性
        public int userid
        {
            get { return _userid; }
            set { _userid = value; }
        }
        public string username
        {
            get { return _username; }
            set { _username = value; }
        }
        public string password
        {
            get { return _password; }
            set { _password = value; }
        }
        public int roleid
        {
            get { return _roleid; }
            set { _roleid = value; }
        }
        public string telephone
        {
            get { return _telephone; }
            set { _telephone = value; }
        }
        public string usersex
        {
            get { return _usersex; }
            set { _usersex = value; }
        }
        public string address
        {
            get { return _address; }
            set { _address = value; }
        }
        public string email
        {
            get { return _email; }
            set { _email = value; }
        }
        public string logintime
        {
            get { return _logintime; }
            set { _logintime = value; }
        }
        public string createtime
        {
            get { return _createtime; }
            set { _createtime = value; }
        }
        private string ReturnAutoID()
        {
            return "userid";
        }
        #endregion
    }
反射方法类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace Reflecti
{
    public class GetModelInfo
    {
        /// <summary>
        /// 通过反射获得对象名称和自动增长ID
        /// </summary>
        /// <param name="obj">对象</param>
        /// <returns>返回string数组</returns>
        public static string[] Core(Object obj)
        {
            string[] str=new string[2];
            Type T = obj.GetType();
            MethodInfo mi = T.GetMethod("ReturnAutoID",
                                        BindingFlags.NonPublic
                                        | BindingFlags.Instance,
                                        null, new Type[] { }, null);
            //通过反射执行ReturnAutoID方法,返回AutoID值
            string str1 = mi.Invoke(obj, null).ToString();
            str[1] = str1;
            str[0] = T.Name.ToString();
            //返回该Obj的名称以及ReturnAutoID的值
            return str;
        }
    }
}


调用:
userinfo _userinfo = new userinfo();
MessageBoxShow("实体类类名:"GetModelInfo.Core(_userinfo)[1] + "    自动ID:"+GetModel.Core(_userinfo)[0]);

评分

参与人数 1技术分 +1 收起 理由
苏波 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马