日期:2014-05-18 浏览次数:21525 次
public class a
{
public string[] b = {"1","2","3"};
}
public class xx
{
public string[] c;
public xx(a _a)
{
c = _a.b;
}
}
class Program
{
static void Main(string[] args)
{
a a = new a();
foreach (string s in a.b)
{
Console.WriteLine(s);
}
xx xx = new xx(a);
foreach (string s in xx.c)
{
Console.WriteLine(s);
}
Console.ReadLine();
}
}
------解决方案--------------------
良好的设计标准不推荐这样做,如果实在要这样做,推荐看看老外的blog。毕竟这不是一个很简单的问题,
http://codebetter.com/blogs/patricksmacchia/archive/2007/09/12/dissecting-an-undeterministic-windows-forms-v2-bug.aspx
可以看看An idea for the fix下面的代码,是怎么读到一个private的成员的。
------解决方案--------------------
不好意思,如果你指要获取私有成员的话:
public class a
{
string[] b = { "1", "2", "3" };
public string[] B
{
get { return b; }
}
}
public class xx
{
string[] c;
public string[] C
{
get { return c; }
}
public xx(a _a)
{
c = ((string[])_a.GetType().GetField("b", BindingFlags.NonPublic|BindingFlags.Instance).GetValue(_a));
}
}
class Program
{
static void Main(string[] args)
{
a a = new a();
foreach (string s in a.B)
{
Console.WriteLine(s);
}
xx xx = new xx(a);
foreach (string s in xx.C)
{
Console.WriteLine(s);
}
Console.ReadLine();
}
}
------解决方案--------------------
如果是只有一个class a的话,为什么不
public class xx:a ------------------------------让xx从a派生呢?到时候直接应用就是了
{
public void xxx(string xxxx)
{
string[] c;
c = 这时要根据参数xxxx的值来确定c指向a.aa还是b.bb,就是不知道这个地方的代码怎么写。
}
}