黑马程序员技术交流社区
标题:
老师,大神看下。
[打印本页]
作者:
周琪
时间:
2013-5-18 12:18
标题:
老师,大神看下。
老师,大神看下。c#中的反射可以获得父类中的私有成员么,有的话请给个例子。百度不到啊。
作者:
许庭洲
时间:
2013-5-18 19:30
[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
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
作者:
吃肉的小浣熊
时间:
2013-5-18 19:56
实体类:
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]);
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2