[seasar-dotnet:624] Re: JScript.NET式について
Kazuya Sugimoto
[E-MAIL ADDRESS DELETED]
2007年 10月 27日 (土) 22:51:02 JST
杉本です。
meiさん、ありがとうございます。
手が空き次第、取り込ませていただきます。
もしかするとデフォルトでOFFにして構成ファイルでONにするような仕組みに
するかもしれません。
assemblyタグで指定したアセンブリを追加していくのは良い案ですね。
07/10/26 に mei<[E-MAIL ADDRESS DELETED]> さんは書きました:
> こんにちは、meiです。
>
> 回答ありがとうございます。
>
> やはり、方法はないですか。とりあえず、Seasar.NETのコードを弄る方向でやってみ
> ます。
> あと、先日のメールで「importを書けば」と書きましたが、
> importだけだと、型がすべて名前空間扱いにされてダメで、
> JScript.NETのコンパイルオプションにアセンブリの参照を追加する必要がありまし
> た。
>
> 参考までに書き換えたコードを貼り付けておきます。
>
> 下記のコードは、app.exe.configのassemblyタグで指定したアセンブリを参照に追加
> し、
> その中の名前空間をimportしています。
>
> -- ここから
> static void SetReferencedAssemblies(CompilerParameters parameters,
> IDictionary<string, object> assemblies)
> {
> string dir = Path.GetDirectoryName(new
> Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath);
> foreach (string asm in assemblies.Keys)
> {
> if (!StringUtil.IsEmpty(asm))
> {
> string name = new AssemblyName(asm).Name;
> string dll = Path.Combine(dir, name + ".DLL");
> string exe = Path.Combine(dir, name + ".EXE");
> if (File.Exists(dll))
> parameters.ReferencedAssemblies.Add(dll);
> else if (File.Exists(exe))
> parameters.ReferencedAssemblies.Add(exe);
> else
> parameters.ReferencedAssemblies.Add(name);
> }
> }
> }
>
> static string GetImports(IDictionary<string, object> assemblies)
> {
> Dictionary<string, object> namespaces = new Dictionary<string,
> object>();
> foreach (Assembly asm in
> AppDomain.CurrentDomain.GetAssemblies())
> {
> if (assemblies.ContainsKey(new
> AssemblyName(asm.FullName).Name))
> foreach (Type t in asm.GetTypes())
> if (t.IsPublic && t.Namespace != null)
> namespaces[t.Namespace] = null;
> }
> StringBuilder imports = new StringBuilder();
> foreach (string ns in namespaces.Keys)
> imports.AppendLine("import " + ns + ";");
> return imports.ToString();
> }
>
> static JScriptUtil()
> {
> CompilerParameters parameters = new CompilerParameters();
> parameters.GenerateInMemory = true;
>
> Dictionary<string, object> assemblies = new Dictionary<string,
> object>();
> S2Section config = S2SectionHandler.GetS2Section();
> if (config != null)
> foreach (string name in config.Assemblys)
> assemblies[name] = null;
>
> string imports = String.Empty;
> if (assemblies.Count > 0)
> {
> SetReferencedAssemblies(parameters, assemblies);
> imports = GetImports(assemblies);
> }
>
> CompilerResults results =
> _provider.CompileAssemblyFromSource(parameters, imports + EVAL_SOURCE);
>
> Assembly assembly = results.CompiledAssembly;
> _evaluateType = assembly.GetType("Seasar.Framework.Util.JScript.
> Evaluator");
> }
> -- ここまで
>
> これでdiconファイル中で<arg>System.Drawing.Color.Blue</arg>とか記述出来るは
> ずです。
>
> -----Original Message-----
> From: [E-MAIL ADDRESS DELETED]
> [mailto:[E-MAIL ADDRESS DELETED]] On Behalf Of Kazuya Sugimoto
> Sent: Friday, October 26, 2007 8:45 AM
> To: [E-MAIL ADDRESS DELETED]
> Subject: [seasar-dotnet:620] Re: JScript.NET式について
>
> 杉本です。
>
> 申し訳ありません。現状では良い方法は無かったと思います。
>
> JScript.NET をもう少し便利にするために JScriptUtil.cs の EVAL_SOURCE に
> import を埋め込めるような仕組みが必要なのかなと思っています。
>
> 仕組みを検討したいと思います。
> また良いアイディアをお持ちの方はご意見ください。
>
> 07/10/25 に meiさんは書きました:
> >
> >
> >
> > こんにちは、meiです。
> >
> >
> >
> > JScript.NET式について質問があります。
> >
> >
> >
> > 以下のようなクラスがあったとします。
> >
> >
> >
> > -- ここから
> >
> > namespace MyLib {
> >
> > public class Person
> >
> > {
> >
> > public static readonly Person SuperMan = new Person("S");
> >
> > public Person(string name)
> >
> > {
> >
> > this.name = name;
> >
> > }
> >
> >
> >
> > string name;
> >
> > public string Name {
> >
> > get { return name; }
> >
> > }
> >
> > }
> >
> > }
> >
> > -- ここまで
> >
> >
> >
> > ここで、Person.SuperManをJScriptから使いたいので以下のように書くと、
> >
> >
> >
> > <initMethod name="xxx">
> >
> > <arg>MyLib.Person.SuperMan</arg>
> >
> > </initMethod>
> >
> >
> >
> > 「変数 'MyLib' が宣言されていません。」
> >
> >
> >
> > と怒られてしまいます。import MyLib;を記述出来ればエラーは出なくなるのです
> が、
> >
> > Seasar.NETのJScript式中では、importを書くことが出来ないようです。
> >
> >
> >
> > また、JScriptUtil.csのEVAL_SOURCE中にimportを記述すれば動作することは確認
> しましたが、
> >
> > Seasar.NETのコードに手を入れることは避けたいところです。
> >
> >
> >
> > 何か良いやり方はありませんでしょうか?
> >
> > よろしくお願いします。
> >
> >
> > _______________________________________________
> > seasar-dotnet mailing list
> > [E-MAIL ADDRESS DELETED]
> > https://ml.seasar.org/mailman/listinfo/seasar-dotnet
> >
> >
>
>
> --
> Kazuya Sugimoto
> Microsoft MVP Visual Developer - Solutions Architect
> http://d.hatena.ne.jp/sugimotokazuya/
> _______________________________________________
> seasar-dotnet mailing list
> [E-MAIL ADDRESS DELETED]
> https://ml.seasar.org/mailman/listinfo/seasar-dotnet
>
> _______________________________________________
> seasar-dotnet mailing list
> [E-MAIL ADDRESS DELETED]
> https://ml.seasar.org/mailman/listinfo/seasar-dotnet
>
--
Kazuya Sugimoto
Microsoft MVP Visual Developer - Solutions Architect
http://d.hatena.ne.jp/sugimotokazuya/
seasar-dotnet メーリングリストの案内