var compilation = Microsoft.CodeAnalysis.CSharp.CSharpCompilation.Create("a") .WithOptions(new Microsoft.CodeAnalysis.CSharp.CSharpCompilationOptions(Microsoft.CodeAnalysis.OutputKind.DynamicallyLinkedLibrary)) .AddReferences( Microsoft.CodeAnalysis.MetadataReference.CreateFromFile(typeof(object).GetTypeInfo().Assembly.Location)) .AddSyntaxTrees(Microsoft.CodeAnalysis.CSharp.CSharpSyntaxTree.ParseText( @" using System; public class C { public C(){} public string M() { return ""Hello Roslyn.""; } }")); var fileName = @"d:\NetStandart\TestCoreNetApp\src\TestCoreNetApp\bin\Debug\netcoreapp1.0\a.dll"; compilation.Emit(fileName); var a = System.Runtime.Loader.AssemblyLoadContext.Default.LoadFromAssemblyPath(fileName); Type = a.GetType("C"); var obj = Activator.CreateInstance(); var res = .GetMethod("M").Invoke(obj, null); Console.WriteLine(res.ToString());
string words = " "; string pattern = @"\w+"; var scr = Microsoft.CodeAnalysis.Scripting.ScriptOptions.Default; var mscorlib = Assembly.Load(System.Runtime.Loader.AssemblyLoadContext.GetAssemblyName(@"c:\Users\Smirnov_SA\.nuget\packages\Microsoft.NETCore.Portable.Compatibility\1.0.1\ref\netcore50\mscorlib.dll")); scr =scr.WithReferences(mscorlib, typeof(MulticastDelegate).GetTypeInfo().Assembly, typeof(System.Runtime.CompilerServices.IStrongBox).GetTypeInfo().Assembly, typeof(MatchEvaluator).GetTypeInfo().Assembly, typeof(Regex).GetTypeInfo().Assembly) .WithImports("System", "System.Text.RegularExpressions"); string = @"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; });"; var result = Microsoft.CodeAnalysis.CSharp.Scripting.CSharpScript.EvaluateAsync(, scr).Result; MatchEvaluator evaluator = (MatchEvaluator)result; Console.WriteLine(Regex.Replace(words, pattern, evaluator));
mscorlib.dll
System.Private.CoreLib.ni.dll
System.Runtime.dll
"Microsoft.CodeAnalysis.CSharp": "2.0.0-beta3",
"Microsoft.CodeAnalysis.CSharp.Scripting": "2.0.0-beta3",
"Microsoft.CodeAnalysis.Scripting.Common": "2.0.0-beta3",
Microsoft.CodeAnalysis.Scripting
= "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; | |});"; = " "; = "\w+"; // MatchEvaluator evaluator = (MatchEvaluator)(); ScriptOptions=("Microsoft.CodeAnalysis.Scripting.ScriptOptions","Microsoft.CodeAnalysis.Scripting"); CSharpScript=("Microsoft.CodeAnalysis.CSharp.Scripting.CSharpScript","Microsoft.CodeAnalysis.CSharp.Scripting"); scr = (ScriptOptions.Default); mscorlib = (.("mscorlib.dll",)); Private_CoreLib=(.("System.Private.CoreLib.ni",)); System_Runtime=(.("System.Runtime",)); RegularExpressions=(.("System.Text.RegularExpressions",)); Regex=(RegularExpressions.GetType("System.Text.RegularExpressions.Regex")); scr =(scr.WithReferences(mscorlib.(), Private_CoreLib.(), System_Runtime.(), RegularExpressions.())); scr =(scr.WithImports("System", "System.Text.RegularExpressions")); evaluator = ((CSharpScript.EvaluateAsync(, scr.())).Result); (Regex.Replace(, , evaluator.()));
public class { public string ; public string ; public int ; public string ; } public class ExcelReader { static Regex = new Regex("[A-Za-z]+"); OpenXmlElementList ; void (string , ) { . = ; var match = .Match(); var = match.Value; var = int.Parse(.Substring(.Length)); . = ; . = ; } void (List<> , Cell cell) { var = cell.CellReference.InnerText; var text = cell.CellValue?.Text; var DataType = cell.DataType; string res = text; if (DataType != null && DataType == CellValues.SharedString) { int ssid = int.Parse(text); res = [ssid].InnerText; } if (res == null) return; var result = new (); (, result); result. = res; .Add(result); } public List<> ReadExcel(string fileName) { List<> = new List<>(); using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { using (SpreadsheetDocument doc = SpreadsheetDocument.Open(fs, false)) { var workbookPart = doc.WorkbookPart; // // var pt = workbookPart.GetPartsOfType<SharedStringTablePart>(); var sstpart = pt.First(); var sst = sstpart.SharedStringTable; = sst.ChildElements; var workbook = workbookPart.Workbook; // var sheet = workbook.Descendants<Sheet>().First(); var worksheetPart = (DocumentFormat.OpenXml.Packaging.WorksheetPart)workbookPart.GetPartById(sheet.Id); var worksheet = worksheetPart.Worksheet; var cells = worksheet.Descendants<Cell>(); // One way: go through each cell in the sheet foreach (Cell cell in cells) { (, cell); } } } return ; } static string (List<> ) { var = ""; var = 0; foreach (var in ) { var = .; var = .Length; if ( > ) { = ; = ; } else if ( == && string.Compare(, , true) > 0) = ; } return ; } public static object (string fileName) { var res = new ExcelReader(); var = res.ReadExcel(fileName); var = .GroupBy( => .).Select( => new { = .Key, = .ToArray() }).OrderBy( => .); var = (); return new { = .ToList(), = }; } } return new Func<string, object>(ExcelReader.);
ScriptOptions=("Microsoft.CodeAnalysis.Scripting.ScriptOptions","Microsoft.CodeAnalysis.Scripting"); CSharpScript=("Microsoft.CodeAnalysis.CSharp.Scripting.CSharpScript","Microsoft.CodeAnalysis.CSharp.Scripting"); scr = (ScriptOptions.Default); mscorlib = (.("mscorlib.dll",)); Private_CoreLib=(.("System.Private.CoreLib.ni",)); System_Runtime=(.("System.Runtime",)); RegularExpressions=(.("System.Text.RegularExpressions",)); OpenXml=(.("DocumentFormat.OpenXml.dll")); Linq=(.("System.Linq", )); FileSystem=(.("System.IO.FileSystem", )); Regex=(RegularExpressions.GetType("System.Text.RegularExpressions.Regex")); scr =(scr.WithReferences(mscorlib.(), Private_CoreLib.(), System_Runtime.(), RegularExpressions.(),OpenXml.(),Linq.(),FileSystem.())); scr =(scr.WithImports("System", "System.Collections.Generic", "System.Linq", "System.IO", "DocumentFormat.OpenXml", "DocumentFormat.OpenXml.Packaging", "DocumentFormat.OpenXml.Spreadsheet", "System.Text.RegularExpressions")); =("").(); = ((CSharpScript.EvaluateAsync(, scr.())).Result); = (.DynamicInvoke()); (.); = ; =.; (.,); =.; =(.); =(.(.())); =1; // .MoveNext() = (.Current); =.; < =+1; .(); ; =+1; =.(); // =(.); =(.(.())); .MoveNext() = (.Current); =.; =.; // 64 1 26 // =.(); .(.(),); ; ;
// //https://code.msdn.microsoft.com/office/CSOpenXmlGetPlainText-554918c3/sourcecode?fileId=71592&pathId=851860130 public class GetWordPlainText : IDisposable { // Specify whether the instance is disposed. private bool disposed = false; // The word package private WordprocessingDocument package = null; /// <summary> /// Get the file name /// </summary> private string FileName = string.Empty; /// <summary> /// Initialize the WordPlainTextManager instance /// </summary> /// <param name="filepath"></param> public GetWordPlainText(string filepath) { this.FileName = filepath; if (string.IsNullOrEmpty(filepath) || !File.Exists(filepath)) { throw new Exception("The file is invalid. Please select an existing file again"); } this.package = WordprocessingDocument.Open(filepath, true); } /// <summary> /// Read Word Document /// </summary> /// <returns>Plain Text in document </returns> public string ReadWordDocument() { StringBuilder sb = new StringBuilder(); OpenXmlElement element = package.MainDocumentPart.Document.Body; if (element == null) { return string.Empty; } sb.Append(GetPlainText(element)); return sb.ToString(); } /// <summary> /// Read Plain Text in all XmlElements of word document /// </summary> /// <param name="element">XmlElement in document</param> /// <returns>Plain Text in XmlElement</returns> public string GetPlainText(OpenXmlElement element) { StringBuilder PlainTextInWord = new StringBuilder(); foreach (OpenXmlElement section in element.Elements()) { switch (section.LocalName) { // Text case "t": PlainTextInWord.Append(section.InnerText); break; case "cr": // Carriage return case "br": // Page break PlainTextInWord.Append(Environment.NewLine); break; // Tab case "tab": PlainTextInWord.Append("\t"); break; // Paragraph case "p": PlainTextInWord.Append(GetPlainText(section)); PlainTextInWord.AppendLine(Environment.NewLine); break; default: PlainTextInWord.Append(GetPlainText(section)); break; } } return PlainTextInWord.ToString(); } #region IDisposable interface public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { // Protect from being called multiple times. if (disposed) { return; } if (disposing) { // Clean up all managed resources. if (this.package != null) { this.package.Dispose(); } } disposed = true; } #endregion public static string GetText(string FileName) { using (var pt = new GetWordPlainText(FileName)) { return pt.ReadWordDocument(); } } } return new Func<string,string>(GetWordPlainText.GetText);
ScriptOptions=("Microsoft.CodeAnalysis.Scripting.ScriptOptions","Microsoft.CodeAnalysis.Scripting"); CSharpScript=("Microsoft.CodeAnalysis.CSharp.Scripting.CSharpScript","Microsoft.CodeAnalysis.CSharp.Scripting"); scr = (ScriptOptions.Default); mscorlib = (.("mscorlib.dll",)); Private_CoreLib=(.("System.Private.CoreLib.ni",)); System_Runtime=(.("System.Runtime",)); RegularExpressions=(.("System.Text.RegularExpressions",)); OpenXml=(.("DocumentFormat.OpenXml.dll")); Linq=(.("System.Linq", )); FileSystem=(.("System.IO.FileSystem", )); Regex=(RegularExpressions.GetType("System.Text.RegularExpressions.Regex")); scr =(scr.WithReferences(mscorlib.(), Private_CoreLib.(), System_Runtime.(),OpenXml.(),FileSystem.())); scr =(scr.WithImports("System", "System.Text", "System.IO", "DocumentFormat.OpenXml", "DocumentFormat.OpenXml.Packaging")); =("").(); = ((CSharpScript.EvaluateAsync(, scr.())).Result); = .DynamicInvoke(); = ; .(); .();
Source: https://habr.com/ru/post/309340/
All Articles