C#动态编译表达式

下面是原始方案
public object ComplierCode(string expression, ILogService logService, out string errorString)
{
string code = WrapExpression(expression);
object retvalue = null;
errorString = “”;
CSharpCodeProvider csharpCodeProvider = new CSharpCodeProvider();
try
{
//编译的参数
CompilerParameters compilerParameters = new CompilerParameters
{
CompilerOptions = “/t:library”,
GenerateInMemory = true
};
//开始编译
CompilerResults compilerResults = csharpCodeProvider.CompileAssemblyFromSource(compilerParameters, code);
if (compilerResults.Errors.Count > 0)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < compilerResults.Errors.Count; i++) { sb.Append(sb.Length > 0 ? “;” : “”);
sb.Append(compilerResults.Errors[i].ErrorText);
}
errorString = sb.ToString();
throw new Exception(“编译出错!” + sb.ToString());
}

Assembly assembly = compilerResults.CompiledAssembly;
Type type = assembly.GetType(“ExpressionCalculate”);
MethodInfo method = type.GetMethod(“Calculate”);
retvalue = method.Invoke(null, null);
}
catch (Exception ex)
{
logService.WriteLog(“编译出错,code=” + code, ex);
}
finally
{
csharpCodeProvider.Dispose();
}
return retvalue;
}

private string WrapExpression(string expression)
{
string code = @”
using System;

class ExpressionCalculate
{
public static object Calculate()
{
return {0};
}
}
“;
return code.Replace(“{0}”, expression);
}
其他方案:https://github.com/davideicardi/DynamicExpresso

Linux CentOS7安装NET Core

sudo rpm -Uvh https://packages.microsoft.com/config/rhel/7/packages-microsoft-prod.rpm
sudo yum update
sudo yum install dotnet-sdk-2.2-2.2.108-1

安装守护进程
yum install supervisor
systemctl enable supervisord.service
systemctl start supervisord.service
systemctl status supervisord.service
vi /etc/supervisord.conf(修改9001端口注释,打开防火墙,重启)
systemctl restart supervisord.service
参考:https://my.oschina.net/weidedong/blog/747428

C#动态执行javascript

Create a file in Notepad with only these contents:

class EvalClass { function Evaluate(expression: String) { return eval(expression); } }
Save the file as C:\MyEval.js

Open a VS2005 Command Prompt (Start, Programs, VS2005, VS2005 Tools)

Type Cd\ to get to C:\

Type

jsc /t:library C:\MyEval.js
A new file is created named MyEval.dll.

Copy MyEval.dll to the project and reference it (also reference Microsoft.Jscript.dll).

Then you should be able to call it like this:

Dim jScriptEvaluator As New EvalClass
Dim objResult As Object
objResult = jScriptEvaluator.Evaluate(“1==1 && 2==2”)
其他方式:https://github.com/davideicardi/DynamicExpresso(支持netcore)

NET之NLOG使用

安装
Install-Package NLog.Config
配置文件

  <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
      autoReload="true"
      throwExceptions="false"
      internalLogLevel="Trace" internalLogToConsole="false" internalLogFile="nlog-internal.log">
  <targets  async="true">
    <target name="logfile" xsi:type="File" fileName="file.txt" />
    <target name="logconsole" xsi:type="Console" />
  </targets>
  <rules>
    <logger name="*" minlevel="Info" writeTo="logconsole,logfile" />
    <logger name="*" minlevel="Debug" writeTo="logconsole" />
    <logger name="*" minlevel="Error" writeTo="logconsole,logfile" />
  </rules>
</nlog>  

使用
public static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
Logger.Error("Error" + (e.Source as AccordionStyleControl).Tag.ToString());

C# winform 自定义透明控件,可以重叠

直接上代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
using System.Drawing.Imaging;

namespace WFControl
{
public partial class ArrowLine : UserControl
{
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x00000020;
return cp;
}
}
public ArrowLine()
{
InitializeComponent();
[subscribe]this.SetStyle(ControlStyles.SupportsTransparentBackColor | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);[/subscribe]
this.SetStyle(ControlStyles.ResizeRedraw, false);
}
private int mlineHeight = 5;
private byte mdirection = 0;

///

/// 线条宽度.
///

[Description(“宽度”)]

public int LineHeight
{
get
{
return mlineHeight;
}
set
{
mlineHeight = value;
}
}
private int mIsArrow = 0;
///

/// 是否显示箭头.
///

[Description(“箭头”)]
public int IsArrow
{
get { return mIsArrow; }
set
{
mIsArrow = value;
}
}
///

/// 线条方向
///0:从右至左;1:从下至上;2:从左至右;3:从上至下.
///

[Description(“类型”)]

public byte Direction
{
get
{
return mdirection;
}
set
{
mdirection = value;
}
}
[Description(“颜色”)]
public Color color
{
get;
set;
}
protected override void OnPaint(PaintEventArgs e)
{
Color drawColor = Color.FromArgb(0, Color.Purple);
Brush brush = new SolidBrush(drawColor);

Pen pen = new Pen(this.color);
pen.Width = LineHeight;
AdjustableArrowCap aac = null;
AdjustableArrowCap aac1 = null;
if (mIsArrow == 1)
{
aac = new AdjustableArrowCap(4, 2);
pen.CustomStartCap = aac;
}
if (mIsArrow == 2)
{
aac = new AdjustableArrowCap(4, 2);
pen.CustomEndCap = aac;
}
if (mIsArrow == 3)
{
aac = new AdjustableArrowCap(4, 2);
aac1 = new AdjustableArrowCap(4, 2);
pen.CustomStartCap = aac;
pen.CustomEndCap = aac1;
}

int lenWidth = mlineHeight;
int x1 = 0;
int y1 = 0;
int x2 = 0;
int y2 = 0;
if (Direction == 0)
{
// 0:从右至左; 1:从下至上;2:从左至右; 3:从上至下.
x1 = this.Size.Width;
y1 = this.Size.Height / 2;
x2 = 0;
y2 = y1;
}
else if (Direction == 1)
{
x1 = this.Size.Width / 2;
y1 = this.Size.Height;
x2 = x1;
y2 = 0;
}
else if (Direction == 2)
{
//2:从左至右;
x1 = 0;
y1 = this.Size.Height / 2;
x2 = this.Size.Width;
y2 = y1;
}
else if (Direction == 3)
{
//3:从上至下.
x1 = this.Size.Width / 2;
y1 = 0;
x2 = x1;
y2 = this.Size.Height;
}

//e.Graphics.FillRectangle(brush, 0, 0, this.Size.Width, this.Size.Height);
e.Graphics.DrawLine(pen, x1, y1, x2, y2);
pen.Dispose();
if (mIsArrow == 1 || mIsArrow == 2)
{
aac.Dispose();
}
if (mIsArrow == 3)
{
aac.Dispose();
aac1.Dispose();
}
brush.Dispose();
}
}
}