Информация об изменениях

Сообщение Re[5]: Ошибка Компиляция Scripting API от 05.09.2016 8:25

Изменено 05.09.2016 9:14 Serginio1

Здравствуйте, Serginio1, Вы писали:

S>Здравствуйте, Sinix, Вы писали:


S>Кстати работает в .Net Core и Scripting API


S>
S>public static void TestRoslynScript()
S>        {
S>            object result = Microsoft.CodeAnalysis.CSharp.Scripting.CSharpScript.EvaluateAsync("1 + 2").Result;
S>            Console.WriteLine(result.ToString());
S>            Console.ReadLine();

S>        }
S>


Но более сложные не работают

 static  String ПолучитьСтрокуДелегата()
        {
            string returnStr = @"return (MatchEvaluator)((match) =>
            {
              string x = match.Value;
            // If the first char is lower case...
        if (char.IsLower(x[0]))
        {
            // Capitalize it.
            return char.ToUpper(x[0]) + x.Substring(1, x.Length - 1);
        }
        return x;

            });";

            return returnStr;

        }


        static MatchEvaluator ПолучитьДелегат()
        {
            return (MatchEvaluator)((match) =>
               {
                   string x = match.Value;
                   // If the first char is lower case...
                   if (char.IsLower(x[0]))
                   {
                       // Capitalize it.
                       return char.ToUpper(x[0]) + x.Substring(1, x.Length - 1);
                   }
                   return x;


               });
               }
        public static void ТестRegex()
        {
           
             string words = "надо заменить все первые буквы в словах на заглавные";
            string pattern = @"\w+";
            // MatchEvaluator evaluator = (MatchEvaluator)ПолучитьДелегат();
            
            var scr = Microsoft.CodeAnalysis.Scripting.ScriptOptions.Default;
            scr=scr.WithReferences(typeof(MulticastDelegate).GetTypeInfo().Assembly, typeof(System.Runtime.CompilerServices.IStrongBox).GetTypeInfo().Assembly, typeof(MatchEvaluator).GetTypeInfo().Assembly);

//            scr = scr.WithReferences(typeof(string).GetTypeInfo().Assembly);
            scr = scr.WithImports("System", "System.Text.RegularExpressions");

                   

              var result = Microsoft.CodeAnalysis.CSharp.Scripting.CSharpScript.EvaluateAsync(ПолучитьСтрокуДелегата(), scr).Result;
              MatchEvaluator evaluator = (MatchEvaluator)result;
           // MatchEvaluator evaluator = ПолучитьДелегат();
             Console.WriteLine(Regex.Replace(words, pattern, evaluator));        }



Выдает ошибку

Необработанное исключение типа "Microsoft.CodeAnalysis.Scripting.CompilationErrorException" в Microsoft.CodeAnalysis.Scripting.dll

Дополнительные сведения: (1,8): error CS0012: The type 'MulticastDelegate' is defined in an assembly that is not referenced. You must add a reference to assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'


Хотя ссылка на typeof(MulticastDelegate).GetTypeInfo().Assembly и какой нафиг mscorlib в .Net Core
Здравствуйте, Serginio1, Вы писали:

S>Здравствуйте, Sinix, Вы писали:


S>Кстати работает в .Net Core и Scripting API


S>
S>public static void TestRoslynScript()
S>        {
S>            object result = Microsoft.CodeAnalysis.CSharp.Scripting.CSharpScript.EvaluateAsync("1 + 2").Result;
S>            Console.WriteLine(result.ToString());
S>            Console.ReadLine();

S>        }
S>


Но более сложные не работают

 static  String ПолучитьСтрокуДелегата()
        {
            string returnStr = @"return (MatchEvaluator)((match) =>
            {
              string x = match.Value;
            // If the first char is lower case...
        if (char.IsLower(x[0]))
        {
            // Capitalize it.
            return char.ToUpper(x[0]) + x.Substring(1, x.Length - 1);
        }
        return x;

            });";

            return returnStr;

        }


        static MatchEvaluator ПолучитьДелегат()
        {
            return (MatchEvaluator)((match) =>
               {
                   string x = match.Value;
                   // If the first char is lower case...
                   if (char.IsLower(x[0]))
                   {
                       // Capitalize it.
                       return char.ToUpper(x[0]) + x.Substring(1, x.Length - 1);
                   }
                   return x;


               });
               }
        public static void ТестRegex()
        {
           
             string words = "надо заменить все первые буквы в словах на заглавные";
            string pattern = @"\w+";

            
            var scr = Microsoft.CodeAnalysis.Scripting.ScriptOptions.Default;
            scr=scr.WithReferences(typeof(MulticastDelegate).GetTypeInfo().Assembly, typeof(System.Runtime.CompilerServices.IStrongBox).GetTypeInfo().Assembly, typeof(MatchEvaluator).GetTypeInfo().Assembly);


            scr = scr.WithImports("System", "System.Text.RegularExpressions");

                   

              var result = Microsoft.CodeAnalysis.CSharp.Scripting.CSharpScript.EvaluateAsync(ПолучитьСтрокуДелегата(), scr).Result;
              MatchEvaluator evaluator = (MatchEvaluator)result;
           // MatchEvaluator evaluator = ПолучитьДелегат();
             Console.WriteLine(Regex.Replace(words, pattern, evaluator));        }



Выдает ошибку

Необработанное исключение типа "Microsoft.CodeAnalysis.Scripting.CompilationErrorException" в Microsoft.CodeAnalysis.Scripting.dll

Дополнительные сведения: (1,8): error CS0012: The type 'MulticastDelegate' is defined in an assembly that is not referenced. You must add a reference to assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'


Хотя ссылка на typeof(MulticastDelegate).GetTypeInfo().Assembly и какой нафиг mscorlib в .Net Core