C# 怎么将类作为参数传递?例如:我定义了实体类X1,X2,X3;还定义了class A{public string ret { get; internal set; }public string msg { get; internal set; }public X1 data { get; internal set; }}class B{public string ret { get; intern
来源:学生作业帮助网 编辑:六六作业网 时间:2025/01/31 06:51:56
C# 怎么将类作为参数传递?例如:我定义了实体类X1,X2,X3;还定义了class A{public string ret { get; internal set; }public string msg { get; internal set; }public X1 data { get; internal set; }}class B{public string ret { get; intern
C# 怎么将类作为参数传递?
例如:我定义了实体类X1,X2,X3;还定义了
class A
{
public string ret { get; internal set; }
public string msg { get; internal set; }
public X1 data { get; internal set; }
}
class B
{
public string ret { get; internal set; }
public string msg { get; internal set; }
public X2 data { get; internal set; }
}
class C
{
public string ret { get; internal set; }
public string msg { get; internal set; }
public X3 data { get; internal set; }
}
能不能将A、B、C三个类合成一个实体类,而将X1、X2、X3作为参数在合并类中的构造函数传入.
如可否这样:
class D
{
System.Type X;
public D(System.Type type)
{
this.X=type;
}
public string ret { get; internal set; }
public string msg { get; internal set; }
public X.GetClass Data { get; internal set; }
}
当然啦,X.GetClass是不存在的,不知能不能类似这样的方法实现?
C# 怎么将类作为参数传递?例如:我定义了实体类X1,X2,X3;还定义了class A{public string ret { get; internal set; }public string msg { get; internal set; }public X1 data { get; internal set; }}class B{public string ret { get; intern
个人觉得有很多种方法:
1、用Object类:
class ABC{
public string ret { get; internal set; }
public string msg { get; internal set; }
public Object data { get; internal set; }
}
2、用dynamic
class ABC{
public string ret { get; internal set; }
public string msg { get; internal set; }
public dynamic data { get; internal set; }
}
3、用泛型
class ABC<T>{
public string ret { get; internal set; }
public string msg { get; internal set; }
public T data { get; internal set; }
}
就你的情况,个人推荐用泛型.