Archive

Archive for the ‘.NET Framework’ Category

.NET中有关string几个特点

2006/01/17 1 comment
首先,来做几个题目.
int a = 5;
int b = 5;
Console.WriteLine(Object.ReferenceEquals(a,b));   //———-1
string aa = "Hello";
string bb = "Hello";
Console.WriteLine(Object.ReferenceEquals(aa,bb)); //———-2
string c = "Hel";
string d = c + "lo";
Console.WriteLine(Object.ReferenceEquals(aa,d)); //———-3
string e = "Hel" + "lo";
Console.WriteLine(Object.ReferenceEquals(aa,e)); //———-4

看看上面的输出分别是什么.

正确结果是Flase,True,False,True
如果你答对了,就不用往下看,如果没有答对,你也可以不用往下看

1. 第一个因为存在隐式装箱,会从值类型装箱成引用类型,会创建新的对象,所以结果是False.
2. String是引用类型,string a="a";和string a=new string("a");是等价的,会创建对象,这样说aa和bb应该是不同的对象啊,为什么会是同一个对象呢?原因是这样的,在.NET中因为string类型是不可变的引用类型,也是使用频率最多的类型之一,如果采取普通的创建对象的方式,会带来很多小的string对象,占据内存空间,影响效率.所以,.NET就采取了一种技巧,称为"字符串池",会把代码中的字符串常量Hash起来,所有同样的字符串都是使用同一个对象,因为string是不可变对象,所以这样做是不会有任何问题.所以结果是True,是同一个对象.
3.我们说.NET会Hash字符串常量,但是如果一个字符串是用变量组成,JIT是会创建一个新的对象,所以是False.
4.如果一个字符串是由字符串常量组成,在编译的时候,编译器会自动将其合成一个字符串,可以用ildasm查看il代码便知.这个时候相当于是一个字符串常量,所以结果是True.

分类: .NET Framework

OracleOleDbProvider的bug?

2006/01/16 发表评论
这两天遇到一个问题,使用sql语句SELECT COUNT(DISTINCT GROUPNAME) FROM TBLMDLFUNCGROUP WHERE
GROUPNAME LIKE ‘%’
调用cmd.ExecuteScalar()方法,发现在执行sql语句的时候,会自动把COUNT(DISTINCT GROUPNAME)变成COUNT(DISTINCTGROUPNAME),中间的空格没有了,导致运行出错.
我怎么跟踪都没发现什么问题,在最后的cmd.CommandText里面的sql都是正确的,最终执行ExecuteScalar()方法的时候就会报错,而换成SqlServer同样的sql是不会报错的,后来改成OracleClient不使用OleDb,也不会报错,难道是OracleOleDb有bug?
我的连接字符串:"Provider=OraOLEDB.Oracle.1;Password=xxx;Persist Security Info=True;User ID=xxx;Data Source=xxx"

我把Provider换成MS提供的OracleOleDb也不会报错,"Provider=MSDAORA.1;Password=xxx;Persist Security Info=True;User ID=xxx;Data Source=xxx",看来真的是Oracle自己的Provider有bug.

分类: .NET Framework

RemotingException在远程传递的时候要注意的问题

2005/12/23 发表评论
首先,必须在配置文件中的customErrors元素必须设置为off,才能将异常传递到远程客户端.否则客户端就只是会收到一个简单的RemotingException,没有任何详细信息.
二,.NET Framework的异常都可以远程传递.如果是自定义的异常必须满足下面三个条件才能远程传递.
  • 实现 ISerializable
  • SerializableAttribute 属性放置在该类上。
  • 实现一个采用 SerializationInfo 对象和 StreamingContext 对象作为参数的反序列化构造函数。

只要注意了这几点,Remoting的异常传递就没有什么问题了,之前我被搞了半天才搞明白.

分类: .NET Framework

NUnit2.24发布

2005/12/21 发表评论
本来我以为在VS2005发布之后NUnit就会被VS2005里面的单元测试工具给挤出市场,但是后来发现好像只有TeamSuite版本才带有单元测试工具,我现在还没有用上MS的,也不知道怎么样.
但是NUnit还算熟悉,本来以为NUnit对2.0不是很支持,但是在VS2005里面安装上了TestDriven.NET(一个可以集成在VS中的Addin,可以使用NUnit等工具进行测试),居然可以非常好的运行,要知道,TD.NET可以一年多都没有更新了.
今天不错,NUnit2.24正式发布,明确支持.Net Framework2.0,分为两个版本下载,一个For .NET Framework1.1的,一个For2.0的,可以根据需要下载
这里下载.

下面是what’s new:

General

  • The /framework option, introduced in NUnit 2.2.3, has been removed. The same functionality is now provided by the clr.bat command file.
  • A number of tests, failing only on certain systems, have been corrected.
    • All tests now pass on Mono 1.1.10, with one exception having to do with FileSystemWatcher. That test is ignored for now, since the watcher is only used in the Windows Gui.
    • A race condition, which caused intermittent failures in the EventPump tests, has been removed.
    • Some culture-specific tests have been corrected
  • Common assembly info has been moved to a single file in the source.

Framework

  • Two new Assert Methods have been added.
    Assert.Greater
    Tests whether its first argument is greater than the second
    Assert.Less
    Tests whether its first argument is less than the second
  • The ExpectedException attribute may now be used with a string argument, representing the full type name of the expected exception. This eliminates the need to have a reference to the assembly defining the exception and will thus allow the test to pass across multiple versions of that assembly.
  • When an error or exception in a test is followed by a second error in the TearDown, both errors are now reported.
  • A custom configuration file, specified in a test project, is no longer ignored.

Console Interface

  • The /noshadow parameter now works, suppressing the use of ShadowCopy for the tests. Note: This cannot be seen when running the NUnit tests themselves, because certain tests enable ShadowCopy in AppDomains that they create.
  • The /out parameter is no longer ignored.
  • The /xml parameter now bases a relative path on the current directory rather than the directory containing the executing assembly.

Forms Interface

  • Test methods inherited from a base class now appear in the gui prefixed by the name of the base class. As a result, a test method in a derived class can redefine a test method in the base class using new without crashing the Gui.
  • Attempting to add a duplicate name to the Gui now gives a warning method as opposed to throwing an exception.
  • Fixed an error that occured when creating a new project from the Gui.
  • Fixed an error in the progress bar, which caused it to remain green when encountering an ignored TestFixture.
  • Provided an advisory message when an assembly that uses the 2.0 framework fails to load under .Net 1.1.

分类: .NET Framework

Remoting Exception传递

2005/12/09 发表评论
    这两天一直在搞Remoting,有个问题困扰很久,就是我在客户端无法捕捉到服务器正确的异常信息,捕捉到的都是一个简单的,RemotingException,"Server encountered an internal error.  For more information, turn on customErrors in the server’s .config file.".google好久,都只需要标记为[Serializable]就行了,但是我标记了,还实现了ISerializable接口,还定义了几个所谓的构造函数,结果还是不行.
    后来还是把这个错误信息的英文在google搜索才知道.原来如果client和server如果不在同一台机器上,.NET默认只会传递一个generic RemotingException,应该是处于安全的因素.如果要知道正确的异常,不是像报错信息所说的将配置文件中的customErrors设置为on,而是要设置为off.真是郁闷,.NET的错误的报错信息,让很多人都走了误区.
分类: .NET Framework
加关注

Get every new post delivered to your Inbox.