ものがたり(旧)

atsushieno.hatenablog.com に続く

System.Orgyを正当化するextern alias

アセンブリ相互参照(orgy)は、好ましくはないけど、別に禁じられているわけでもない。かくして、.NET 2.0では、System.dllとSystem.Configuration.dllの間で、好ましくないその因縁が深まっているらしい。*1

System.Configuration.dllをビルドするには、System.dllをビルドしなければならないのだが、System.dllにあるいくつかのクラス、たとえばSystem.Net.Configuration.ModuleElementには、System.Configuration.dllのSystem.Configuration.ConfigurationElementが必要になる。*2

めんどっちいのは、これをbootstrapとfullのビルド2回転で実現しようとすると、System.Configuration.dllをビルドするときに、bootstrapのSystem.dllにあるNameValueCollectionとfullのSystem.dllにあるNameValueCollectionの2つが区別できないというエラーが出る(CS0029の一類型)。

それで、解決方法を探していて、extern aliasとゆー非常にマイナーな機能を見つけたのだけど、mcsのサポートはまだ完全ではないようだ

これと関係してTypeForwardedToAttributeというのも気になっていて、MSこれ使ってるんじゃねーの?と思うのだけど、corcompareの結果を見る限り全く使っていないようだ追記のとおりで、実際には不明*3

System.Runtime.CompilerServicesには、やっつけ用のクラスがいろいろ増えていそうな予感がする。

追記:

生成されたdllに残らないというのでビンゴらしい。



using System.Runtime.CompilerServices;
using System;
using System.Reflection;

[assembly:CLSCompliant (true)]
[assembly:TypeForwardedTo (typeof (string))]
public class Test
{
public static void Main ()
{
foreach (Attribute a in
Assembly.GetExecutingAssembly ()
.GetCustomAttributes (true))
Console.WriteLine (a.GetType ());
}
}


$ csc typeforwardedtoattribute.cs -nologo

$ mono typeforwardedtoattribute.exe
System.Runtime.CompilerServices.RuntimeCompatibilityAttribute
System.Runtime.CompilerServices.CompilationRelaxationsAttribute
System.CLSCompliantAttribute

$ gmcs typeforwardedtoattribute.cs

$ mono typeforwardedtoattribute.exe
System.CLSCompliantAttribute
System.Runtime.CompilerServices.RuntimeCompatibilityAttribute
System.Runtime.CompilerServices.TypeForwardedToAttribute

*1:ていうかSystem.Configuration.dllって、System.dllとSystem.Xml.dllのSystem.Orgyを回避するために作られたんじゃなかったのか…?

*2:ちなみにコレは2.0だけの新しい問題。System.dllとSystem.Xml.dllの場合、System.Xml.dllを2度ビルドする必要は無い。

*3:あるいは、生成されたdllには残らないのかもしれない。