关于C#的构造器问题(100分)

  • 主题发起人 riversoft
  • 开始时间
R

riversoft

Unregistered / Unconfirmed
GUEST, unregistred user!
public class NotSupportPropertyInterface:Exception {
public NotSupportPropertyInterface(string message,string[] propertyList) {}
}
我想构造一个传入参数的异常对象 类似 Delphi里面的 Excception.CreateFmt
不知要如何定义,
那位C#的高手指点一下
 
没有人回答,自己 up 一下
 
你试试:
public class NotSupportPropertyInterface:Exception
{
public NotSupportPropertyInterface(string message,string[] propertyList):base(message)
{ }
}
其中 base(message)是调用父类的某个构造器
c#中不能在构造函数体内调用父类的构造函数,
如果不显式指明,它默认是执行了
public class NotSupportPropertyInterface:Exception
{
public NotSupportPropertyInterface(string message,string[] propertyList):base()
{ }
}
其中base()代表父类的默认构造函数.
 
to goaha:
public NotSupportPropertyInterface(string message,string[] propertyList):base(message)
这点我知道,但是这样我的 propertyList 要如何体现出来呢,
我看到Exception内部有一个虚构函数可是进行重载的
public virtual void GetObjectData(
SerializationInfo info,
StreamingContext context
);
但是却不知道怎么用
我是想把异常信息直接封装在异常对象里面,
我想的信息是 message + propertyList
例如 for (int i=0;i<propertyList.length;i++)
message+=propertyList
如果不能实现只有在每个类里面增加一个方法来处理这段代码
然后在把处理完毕的异常信息传递给 异常对象了,
不知道好有没有更好的解决方法
 
你试试以下的方法,思路是定义了另外一个类(abc)对message及propertyList进行预处理
public class NotSupportPropertyInterface:Exception
{
public NotSupportPropertyInterface(string message,string[] propertyList):
base((new abc(message,propertyList)).NewExcetionMessage())
{}
}
public class abc
{
private string Message;
private string[] PropertyList;
public abc(string message,string[] propertyList)
{
this.Message=message;
this.PropertyList=propertyList;
if (this.Message==null)
{
this.Message="";
}
}
public string NewExcetionMessage()
{
//编写错误信息处理代码,举例如下
string tmpstr;
tmpstr=this.Message;
if (this.PropertyList!=null)
{
for (int i=0;i<this.PropertyList.Length;i++)
{
tmpstr+=this.PropertyList;
}
}
return tmpstr;
}
}
 
to goaha:
谢谢你的参与,其实这样的方法是可以解决问题的,
我的意思是C#遇到类似这样的问题不是都要这样解决吧,
我难道这样限制的原因有什么好处呢,
想java和delphi构造函数都可以作这样的处理,
我只是想知道类似这样的问题能不能在同一个类里面解决,
我想肯定有解决的方法,可能是我们不知道而已,
如果借助Adapterl来解决的话岂不是很麻烦,我不知道M$这样
作的目的是为了什么,很不理解而已,因为类似的问题我经常要遇到
 
有钱没人要?
偶没钱,给偶吧!
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
959
SUNSTONE的Delphi笔记
S
S
回复
0
查看
779
SUNSTONE的Delphi笔记
S
顶部