정말로 C#을 허용 하시겠습니까?
그렇다면 this script engine for C#을보고 싶을 수 있습니다. 임의의 코드를 실행할 수 있다는 것은 큰 보안 노출입니다.
.NET 4.0 (베타 버전, 지금 출시 예정)을 기다리는 것이 좋습니다.이 경우 DLR 또는 동적 언어 런타임을 활용할 수 있습니다.
반면에 .NET 4.0에 의존하고 싶지 않고 자신을 제한하고 싶다면 Reflection을 매우 간단하게 사용할 수 있습니다. 예를 들어 DateTime 인스턴스의 메서드에 자신을 한정시킬 수 있다면 더 간단해질 것입니다. 이 예제에서는 NOW로 설정된 DateTime 인스턴스에서 리플렉션을 통해 메서드를 호출하는 방법을 보여줍니다.
DateTime d = DateTime.Now;
string logicToInvoke= "Now.AddDays(12)";
string pattern = @"^Now\.(?<methodname>\w+)\((?<argstring>[^\)]*)\)";
RGX.Match match = RGX.Regex.Match(logicToInvoke, pattern);
// If match not found, logic is improperly formed.
if (!match.Success)
{
Console.WriteLine("The DateTime method logic is improperly formed.");
return;
}
// Store method and arg
string methodName = (string) match.Groups["methodname"].Value;
string argString = (string) match.Groups["argstring"].Value;
Console.WriteLine("Invoking method '{0}' with args '{1}'...", methodName, argString);
System.Type t= d.GetType();
try
{
switch (methodName)
{
case "AddDays" :
int daysToAdd = System.Int32.Parse(argString);
DateTime result = (DateTime) t.InvokeMember (methodName,
System.Reflection.BindingFlags.Public |
System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.InvokeMethod ,
null, d, new object [] {daysToAdd});
Console.WriteLine("Result: {0}", result.ToString("yyyy/MM/dd"));
break;
default:
Console.WriteLine("unsupported method: {0}", methodName);
break;
}
}
catch (System.Exception exc1)
{
Console.WriteLine("Exception while invoking method: {0}", exc1);
}
DateTime 값의 형식을 그 값과 별도로 유지하는 것이 좋습니다. 시간 ("YY/MM은/DD")의 형식은 더 나은와 마찬가지로 속성에 지정 될 수 있습니다
<Name>DateOne</Name>
<Value format="MM/dd/yy">[Now.AddDays(-1)]</Value>
정말 모든 C# 코드를 허용하고 싶습니까? 큰 보안 구멍처럼 보입니다. – Cheeso
인트라넷입니다. – JonathanWolfson
답변을 찾고 있는데 참조도 아니고 제안도 아닙니다. – JonathanWolfson