有没有办法使用C#CodeDom生成字典初始化程序?那些支持吗?
我想拥有:
private IDictionary<string, string> map = new Dictionary<string, string>
{
{ "Name", "Value" },
...
};
Run Code Online (Sandbox Code Playgroud) 我使用CodeDom动态生成一些c#代码.我想在命名空间中添加一个类型别名.就像是:
namespace MyNameSpace
{
using Timer = System.Threading.Timer;
...
}
Run Code Online (Sandbox Code Playgroud)
我能够创建命名空间但不知道如何创建类型别名.代码到目前为止:
CodeCompileUnit unitCompile = new CodeCompileUnit();
CodeNamespace nsScript = new CodeNamespace("MyNamespace");
unitCompile.Namespaces.Add(nsScript);
Run Code Online (Sandbox Code Playgroud)
如何添加"using Timer = System.Threading.Timer;" 命名空间的语句?
我正在尝试使用CodeDOM创建扩展方法.似乎没有任何支持它们,并且ExtensionAttribute不允许使用(内部使用C#标记扩展方法).
可以使用技巧来指定this修饰符,但是如何创建包含类static,以便代码实际编译?
由于static是C#概念,因此不会通过CodeDOM API公开.并设置TypeAttributes到TypeAttributes.Abstract | TypeAttributes.Sealed | TypeAttributes.Public不行的,因为
抽象类不能被密封或静态
如何使扩展方法编译?
我有大约500个由旧应用程序从XML文件动态生成的WinForms.我想自动将这些转换为我可以在设计器中编辑的VB或C#代码.
我可以手工完成,这需要永远......
或编写装入每个XML,调用旧的应用程序的XML到WinForm的建设者将其转换为一个完整的WinForm的应用程序,并检查每个控件的属性和使用StringBuilder来生成代码,设计师会吞噬.充其量只会是艰巨的.
但是:我想使用设计师使用的相同代码.我认为这将节省我的时间与角落案件.
我想要做的是,采取已建成的WinForm对象,以控制阵列全子控件(由旧的应用程序创建的XML到WinForm的代码),在IDE使用序列化设计的形式相同的代码把它到CodeDom,并获取我可以用真实语言保存的CodeDom语句列表.一旦我拿到CodeDOM,我会很高兴!
这个Codeproject文章体现了我想要做的事情的"概念":从一个完整的表单开始,将其转换('序列化')作为代码.然而,它有两个缺少:(1)它产生使用模板/ StringBuilder的.aspx页,而且那里的属性是(对于web表单细,但WinForms的串行化性质成实际.Designer.vb文件); (2)它是从头开始做到这一切的.我想重新使用visual studio的例程.他们允许您使用许多内容执行此操作,例如,属性网格.我只是想找到一篇文章的链接(也许我的google-fu太弱了),或者是某个人已经做过的短代码示例.
我正在创建一个设计器表面并将控件加载到运行时。将控件反序列化/加载到运行时时遇到问题。
我尝试过的所有方法似乎都有某种类型的问题。
发行面临例如:
我在 git 上创建了一个示例项目:Surface Designer Test
有主要的代码片段:
从设计时序列化
private void LoadRuntime(int type)
{
var controls = surface.ComponentContainer.Components;
SerializationStore data = (SerializationStore)surface.
_designerSerializationService.Serialize(controls);
MemoryStream ms = new MemoryStream();
data.Save(ms);
SaveData.Data = ms.ToArray();
SaveData.LoadType = type;
new RuntimeForm().Show();
}
public object Serialize(System.Collections.ICollection objects)
{
ComponentSerializationService componentSerializationService =
_serviceProvider.GetService(typeof(ComponentSerializationService)) as
ComponentSerializationService;
SerializationStore returnObject = null;
using (SerializationStore serializationStore =
componentSerializationService.CreateStore())
{
foreach (object obj in objects)
{
if (obj is Control control)
{
componentSerializationService.SerializeAbsolute(serializationStore, obj);
}
returnObject …Run Code Online (Sandbox Code Playgroud) 我需要这样做.
public class MyClass{
private static IDictionary<String, Type> databaseAccessClasses
= new Dictionary<String, Type>();
private static IDictionary<String, Type> DatabaseAccessClasses
{
get { return DataAccessFactory.databaseAccessClasses; }
set { DataAccessFactory.databaseAccessClasses = value; }
}
}
Run Code Online (Sandbox Code Playgroud) 如何通过CodeDom编写VB.NET Windows窗体应用程序?
我已经尝试了所有的东西,我得到的最接近的是下面的代码,首先显示命令提示窗口不好,然后显示表格等一秒钟,一切都消失了.还有另一种正确的方法吗?非常感谢一个例子.
Public Module MyApp
Public Sub main()
Dim NewForm As New System.Windows.Forms.Form
NewForm.Name = "Form1"
NewForm.Text = "Form1"
NewForm.Width = 300
NewForm.Height = 300
NewForm.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedSingle
NewForm.ControlBox = True
NewForm.MaximizeBox = False
NewForm.MinimizeBox = True
NewForm.Show()
End Sub
End Module
Run Code Online (Sandbox Code Playgroud) 我正在开发一个应用程序,我想动态生成用于数值计算的代码(用于性能).将此计算作为数据驱动操作太慢.要描述我的要求,请考虑以下类:
class Simulation
{
Dictionary<string, double> nodes;
double t, dt;
private void ProcessOneSample()
{
t += dt;
// Expensive operation that computes the state of nodes at the current t.
}
public void Process(int N, IDictionary<string, double[]> Input, IDictionary<string, double[]> Output)
{
for (int i = 0; i < N; ++i)
{
foreach (KeyValuePair<string, double[]> j in Input)
nodes[j.Key] = j.Value[i];
ProcessOneSample();
foreach (KeyValuePair<string, double[]> j in Output)
j.Value[i] = nodes[j.Key];
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想要做的是JIT编译一个在Process中实现外部循环的函数.定义此函数的代码将由当前用于实现ProcessOneSample的数据生成.为了澄清我的期望,我希望所有的字典查找都在编译过程中执行一次(即JIT编译将直接绑定到字典中的相关对象),这样当编译的代码实际上是执行后,好像所有查找都已经过硬编码.
我想弄清楚的是解决这个问题的最佳工具.我问这个问题的原因是因为有很多选择:
我想用CodeDOM生成C#代码文档.
没有文档的代码:
public class MyType {
public static BitmapImage File {
get { return GetFile("..."); }
}
}
Run Code Online (Sandbox Code Playgroud)
代码与文档:
/// <summary> Gets the File from the location </summary>
public class MyType {
public static BitmapImage File {
get { return GetFile("..."); }
}
}
Run Code Online (Sandbox Code Playgroud)
要么
/// <summary>
/// Gets the File from the location
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public class MyType {
public static BitmapImage File {
get { return GetFile("..."); }
} …Run Code Online (Sandbox Code Playgroud) 我正在使用CSharpCodeProvider来编译一个带有可变参数的.exe.编译工作正常(不返回错误)并成功,但在运行时启动并立即退出无错误或输出.当更改"Main"(例如更改为private或通过重命名)时,编译器输出没有有效的Main方法,因此示例代码不应该是原因.
有没有人对此有答案/解决方案?我很遗憾在这一点,并希望任何有用的回应.提前谢谢〜
*编辑:
编译.exe输出:http://imgur.com/a/WBvz3
编译:
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Resources;
using System.Security.Cryptography;
using System.Text;
using Microsoft.CSharp;
using Packer.Properties;
namespace Packer
{
internal class Program
{
private static void Main(string[] args)
{
Console.WriteLine("Sample Compiler");
Console.WriteLine(".ico-path: ");
var icon = "E:\\sample.ico"; //Console.ReadLine();
Console.WriteLine("> " + icon);
Console.WriteLine("Target-exe: ");
var target = "E:\\sample.exe"; //Console.ReadLine();
Console.WriteLine("> " + target);
var source = Resources.samplesource;
// Compile with all params
var success = CompileFromSource(source, target, icon); …Run Code Online (Sandbox Code Playgroud)