追記
↑の結論の実証コードを書いてみた。
- string.emptyと""の参照比較を行うDLLを作る
- コンソールアプリを作り、最初にstring.Intern( string.Empty )する
- その後Assembly.LoadFromでDLLをロードして参照比較を行う
コンソールアプリのコードは↓
using System; using System.IO; using System.Reflection; namespace RegisterInternPoolEmptyString { class MainClass { [STAThread] public static void Main(string[] args) { string.Intern( string.Empty ); string dllPath = Path.Combine( Directory.GetCurrentDirectory(), "CompareStringEmpty.dll" ); try { Assembly dllAsm = Assembly.LoadFrom( dllPath ); object objCls = dllAsm.CreateInstance( "CompareStringEmpty.CompareStringEmpty" ); Type typeCls = objCls.GetType(); MethodInfo mi = typeCls.GetMethod( "CompareEmptyStringObjectRef" ); bool comparison = (bool)mi.Invoke( objCls, null ); Console.WriteLine( comparison ); } catch( Exception e ) { Console.WriteLine( e ); } } } }
DLLのコードは↓
using System; namespace CompareStringEmpty { public class CompareStringEmpty { public CompareStringEmpty() { } public bool CompareEmptyStringObjectRef() { return (object)string.Empty == (object)""; } } }
実行結果は予想どうりTrueが帰ってきた。