====== Compile and Run ====== using System.Reflection; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; private void displaySyntax(TreeNodeCollection target, SyntaxNode node) { TreeNode branch = new TreeNode (); branch.Text = node.ToString (); branch.Tag = node; target.Add (branch); foreach (SyntaxNode item in node.ChildNodes()) displaySyntax (branch.Nodes, item); } private void runMenu_Click(object sender, EventArgs e) { info.Clear(); tabs.SelectedTab = editorPage; string source = edit.Text; var syntaxTree = CSharpSyntaxTree.ParseText(source); string code = syntaxTree.ToString(); // print(code); displaySyntax(tree.Nodes, syntaxTree.GetRoot()); CSharpCompilation compilation = CSharpCompilation.Create( "assemblyName", new[] { syntaxTree }, new[] { MetadataReference.CreateFromFile(typeof(object).Assembly.Location) }, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); using (var dllStream = new MemoryStream()) using (var pdbStream = new MemoryStream()) { var emitResult = compilation.Emit(dllStream, pdbStream); if (! emitResult.Success) { print("ERROR"); foreach (var diagnostic in emitResult.Diagnostics) print (diagnostic.ToString()); } else { print("O.K."); dllStream.Seek(0, SeekOrigin.Begin); // Load the compiled assembly var assembly = Assembly.Load (dllStream.ToArray()); // Execute the library code var libraryClassType = assembly.GetType("MyNamespace.MyClass"); var libraryInstance = Activator.CreateInstance (libraryClassType); var libraryMethod = libraryClassType.GetMethod ("MyMethod"); var result = libraryMethod.Invoke (libraryInstance, null); print ("RESULT: " + result); } foreach (var diagnostic in emitResult.Diagnostics) { Console.WriteLine(diagnostic.ToString()); } } } edit.Text = @" namespace MyNamespace { public class MyClass { public string MyMethod () { return ""Hello, World!""; } } } "; ===nuget packages=== https://dist.nuget.org/win-x86-commandline/latest/nuget.exe nuget restore **Pripadne vymazat c:\Users\...\AppData\Roaming\NuGet\NuGet.Config** Registered Sources: 1. nuget.org [Enabled] https://api.nuget.org/v3/index.json ... ==c:\Users\...\AppData\Roaming\NuGet\NuGet.Config== ===Design-And-Compile from repository pw-2023=== ==Designer.csproj from Visual Studio 2022== WinExe net6.0-windows enable true enable ==packages Microsoft.CodeAnalysis.CSharp (4.8.0)== Solution Explorer, Designer project, right click, Manage Nuget Packages ===Compiler from repository pw-sharpdevelop=== ==packages.config from pw-sharpdevelop==  ==compiler.csproj from pw-sharpdevelop== packages\Microsoft.CodeAnalysis.CSharp.3.3.1\lib\netstandard2.0\Microsoft.CodeAnalysis.CSharp.dll https://stackoverflow.com/questions/32769630/how-to-compile-a-c-sharp-file-with-roslyn-programmatically https://stackoverflow.com/questions/4181668/execute-c-sharp-code-at-runtime-from-code-file https://www.linkedin.com/pulse/load-compile-run-c-code-dynamically-munib-butt https://github.com/dotnet/roslyn/blob/main/src/Compilers/CSharp/Portable/Compilation/CSharpCompilation.cs#L340 dotnet7.0-7.0.104-1.fc38.src.rpm https://github.com/roslynpad/roslynpad/blob/main/src/RoslynPad.Roslyn/Compiler.cs#L63 https://www.nuget.org/downloads https://dist.nuget.org/win-x86-commandline/latest/nuget.exe ====== Selected properties ====== namespace Designer { internal interface ISelectedProperties { List selectedProperites(); } } public partial class Box : UserControl, ISelectedProperties { List ISelectedProperties.selectedProperites() { return new List { "BackColor" }; } } using System; using System.Collections.Generic; // List using System.ComponentModel; // ICustomTypeDescriptor // https://www.codeproject.com/Articles/13342/Filtering-properties-in-a-PropertyGrid namespace Designer { public class ObjectWrapper : ICustomTypeDescriptor { private object obj = null; private List selected_names = new List() { "Name", "Text", "BackColor", "Size" }; public ObjectWrapper (object param) { obj = param; } #region ICustomTypeDescriptor Members public PropertyDescriptorCollection GetProperties (Attribute[] attributes) { return GetProperties (); } public PropertyDescriptorCollection GetProperties () { PropertyDescriptorCollection input = TypeDescriptor.GetProperties(obj); PropertyDescriptor[] inputArray = new PropertyDescriptor[input.Count]; input.CopyTo (inputArray, 0); PropertyDescriptor[] outputArray = new PropertyDescriptor[1]; outputArray[0] = inputArray[0]; PropertyDescriptorCollection output = new PropertyDescriptorCollection(outputArray, true); List names = selected_names; if (obj is ISelectedProperties) names = (obj as ISelectedProperties).selectedProperites(); foreach (PropertyDescriptor item in input) if (names.Contains (item.Name)) output.Add (item); return output; } public AttributeCollection GetAttributes () { return TypeDescriptor.GetAttributes (obj, true); } public String GetClassName () { return TypeDescriptor.GetClassName (obj, true); } public String GetComponentName () { return TypeDescriptor.GetComponentName (obj, true); } public TypeConverter GetConverter () { return TypeDescriptor.GetConverter (obj, true); } public EventDescriptor GetDefaultEvent () { return TypeDescriptor.GetDefaultEvent (obj, true); } public PropertyDescriptor GetDefaultProperty () { return TypeDescriptor.GetDefaultProperty (obj, true); } public object GetEditor (Type editorBaseType) { return TypeDescriptor.GetEditor (this, editorBaseType, true); } public EventDescriptorCollection GetEvents (Attribute[] attributes) { return TypeDescriptor.GetEvents (obj, attributes, true); } public EventDescriptorCollection GetEvents () { return TypeDescriptor.GetEvents (obj, true); } public object GetPropertyOwner (PropertyDescriptor pd) { return obj; } #endregion } } ===== ICustomTypeDescriptor ===== https://github.com/dotnet/runtime/blob/main/src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/ICustomTypeDescriptor.cs https://github.com/microsoft/VSSDK-Extensibility-Samples/blob/master/ArchivedSamples/WPF_Toolwindow/C%23/CustomTypeDescriptor.cs https://github.com/MatthewKing/DynamicDescriptors/tree/main ===== WinForms Designer ===== https://github.com/dotnet/winforms/tree/main/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.design.iwindowsformseditorservice https://github.com/mono/mono/tree/main/mcs/class/System.Windows.Forms/System.Windows.Forms.Design https://github.com/dotnet/winforms/blob/main/src/System.Windows.Forms.Design/src/System.Windows.Forms.Design.csproj https://github.com/KlausLoeffelmann/NetControlDesigners https://devblogs.microsoft.com/dotnet/state-of-the-windows-forms-designer-for-net-applications/ https://www.nuget.org/packages/Microsoft.WinForms.Designer.SDK https://github.com/microsoft/winforms-designer-extensibility https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.design https://github.com/dotnet/winforms/tree/main/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design https://github.com/microsoft/CsWin32 ===== DesignSurface ===== https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.design.designsurface https://github.com/dotnet/winforms/blob/main/src/System.Windows.Forms.Design/src/System/ComponentModel/Design/DesignSurface.cs https://github.com/mono/mono/blob/main/mcs/class/System.Design/System.ComponentModel.Design/DesignSurface.cs https://www.codeproject.com/Articles/24385/Have-a-Great-DesignTime-Experience-with-a-Powerful https://www.codeproject.com/Articles/1174241/DesignSurfaceExtended-the-third-chapter-how-to-imp https://github.com/ssickles/archive/tree/master/samples https://github.com/ssickles/archive/blob/master/samples/WCFWFSamples/WF/Applications/TrackingProfileDesigner/CS/WorkflowDesignerControl/WorkflowDesignerControl.cs#L112 https://supportcenter.devexpress.com/ticket/details/s136005/add-new-windows-form-control-of-designsurface https://csharp.hotexamples.com/examples/-/DesignSurface/-/php-designsurface-class-examples.html ====== Color button ====== namespace Designer { public partial class ColorButton : ToolStripButton // UserControl { private Color color; public ColorButton (Color c, String name = "") { InitializeComponent (); color = c; const int size = 32; Bitmap img = new Bitmap (size, size); Graphics g = Graphics.FromImage (img); g.FillEllipse (new SolidBrush (color), 1, 1, size - 2, size - 2); this.Image = img; // this.Text = name; this.ToolTipText = name; this.MouseDown += ColorButton_MouseDown; } private void ColorButton_MouseDown (object sender, MouseEventArgs e) { DoDragDrop (color, DragDropEffects.All); } } } ====== Factory button ====== namespace Designer { public class Factory : ToolStripButton { private Type ComponentType; private string ComponentName; public Factory(Type type0) { ComponentType = type0; ComponentName = ComponentType.Name; this.Text = ComponentName; this.ToolTipText = ComponentName; // icon AttributeCollection attrCol = TypeDescriptor.GetAttributes (ComponentType); ToolboxBitmapAttribute imageAttr = attrCol [typeof (ToolboxBitmapAttribute)] as ToolboxBitmapAttribute; if (imageAttr != null) { this.Image = imageAttr.GetImage (ComponentType); this.Text = ""; } this.MouseDown += this.Factory_MouseDown; } private void Factory_MouseDown(object sender, MouseEventArgs e) { DoDragDrop (this, DragDropEffects.All); } public object createInstance () { object result = Activator.CreateInstance (ComponentType); if (result is Box) { Box box = result as Box; box.Width = 80; box.Height = 40; } else if (result is Panel) { Panel p = result as Panel; p.Width = 80; p.Height = 40; p.BackColor = Color.CornflowerBlue; p.BorderStyle = BorderStyle.Fixed3D; } return result; } } } ====== Drag and Drop ====== private void Box_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(typeof(Color))) e.Effect = DragDropEffects.Move; else if (e.Data.GetDataPresent (DataFormats.Text)) e.Effect = DragDropEffects.Copy; else if (e.Data.GetDataPresent(typeof (ToolStripItem))) e.Effect = DragDropEffects.Copy; // else // e.Effect = DragDropEffects.None; /* if (info != null) foreach (var f in e.Data.GetFormats ()) { info.AppendText(f.ToString() + "\r\n"); } */ } private void Box_DragDrop(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(typeof(Color))) { BackColor = (Color) e.Data.GetData(typeof(Color)); } else if (e.Data.GetDataPresent (DataFormats.Text)) { string s = (string) e.Data.GetData(DataFormats.Text); Button btn = new Button(); btn.Text = s; btn.Parent = this; Point location = PointToClient (new Point (e.X, e.Y)); btn.Location = location; } else if (e.Data.GetDataPresent(typeof (ToolStripItem))) { ToolStripItem tool = e.Data.GetData (typeof (ToolStripItem)) as ToolStripItem; if (tool is Factory) { Factory factory = tool as Factory; Point location = PointToClient (new Point(e.X, e.Y)); Object obj = factory.createInstance (); if (obj is Control) { Control ctl = obj as Control; ctl.Location = location; ctl.Parent = this; // new Adapter (ctl); } } } }