ERLEDIGT
NEIN
NEIN
ANTWORTEN
3
3
ZUGRIFFE
1021
1021
EMPFEHLEN
-
03.10.08 10:03 #1
- Registriert seit
- Mar 2006
- Beiträge
- 256
Hallo ich befasse mich zur Zeit mit .NET Scripting und bin schon auf ein Problem gestoßen,
Ich möchte in meinem Programm eine Zeile in meiner Anwendung als Script ausführen.
In meinee Scriptzeile möchte ich ein Objekt(ScriptElements) über eine Variable x nutzen,
hier mein Code:
Mein Problem ist nun ganz einfach, wenn ich das laufen lasse bekomme ich über die MessageBox folgenden Fehler:PHP-Code:namespace Scripting
{
public class ScriptingSample
{
public void Main(string[] args)
{
string sScriptLine = "x.Date();";
ScriptElements elements = new ScriptElements();
ScriptManager manager = new ScriptManager();
Console.WriteLine(manager.RunScript(elements, sScriptLine));
Console.ReadKey();
}
}
#region ScriptManager
public class ScriptManager
{
List<string> arrList = new List<string>();
public delegate string ScriptRun(ScriptElements x);
public ScriptRun runDelegate;
public ScriptManager()
{
arrList.Clear();
arrList.Add("System.dll");
arrList.Add("System.Windows.Forms.dll");
}
public string RunScript(ScriptElements x, string sScriptLine)
{
string sRes = "";
CodeCompileUnit compileUnit = new CodeCompileUnit();
CodeNamespace nameSpace = new CodeNamespace("Scripting");
CodeTypeDeclaration reletionClass = new CodeTypeDeclaration("ScriptClass");
CodeMemberMethod calculationFunction = new CodeMemberMethod();
CompilerResults compilerResults = null;
object objFunc;
compileUnit.Namespaces.Add(nameSpace);
compileUnit.ReferencedAssemblies.AddRange(arrList.ToArray());
nameSpace.Imports.Add(new CodeNamespaceImport("System"));
nameSpace.Imports.Add(new CodeNamespaceImport("System.Windows.Forms"));
nameSpace.Types.Add(reletionClass);
reletionClass.IsClass = true;
reletionClass.TypeAttributes = System.Reflection.TypeAttributes.Public;
reletionClass.Members.Add(calculationFunction);
calculationFunction.Attributes = MemberAttributes.Public;
calculationFunction.ReturnType = new CodeTypeReference(typeof(string));
calculationFunction.Name = "Run";
calculationFunction.Parameters.Add(new CodeParameterDeclarationExpression(typeof(ScriptElements), "x"));
calculationFunction.Statements.Add(new CodeMethodReturnStatement(new CodeSnippetExpression(sScriptLine)));
CompilerParameters compilerParameters = new CompilerParameters();
compilerParameters.GenerateInMemory = true;
CSharpCodeProvider csharp = new CSharpCodeProvider();
compilerResults = csharp.CompileAssemblyFromDom(compilerParameters, compileUnit);
if ((compilerResults != null) && (compilerResults.Errors.Count > 0))
{
for (int i = 0; i < compilerResults.Errors.Count; i++)
{
MessageBox.Show(compilerResults.Errors[i].ToString());
}
}
else
{
objFunc = compilerResults.CompiledAssembly.CreateInstance("Scripting.ScriptClass", true);
runDelegate = (ScriptRun)Delegate.CreateDelegate(typeof(ScriptRun), objFunc, calculationFunction.Name);
if (runDelegate != null)
sRes = runDelegate(x);
}
return sRes;
}
}
#endregion
#region ScriptElements
public class ScriptElements
{
public ScriptElements()
{ }
public string Date()
{
return DateTime.Now.ToString();
}
}
#endregion
}
Der Typ- oder namespacename ScriptElements ist im Namespace Scripting nicht vorhanden. (Fehlt eine Assemblyverweis?).
Wie bekomme ich das ganze zum laufen?
Wenn ich ScriptElements durch z.b. float ersetze, zum rechnen, läuft es wunderbar.
-
Hi
Du fügst der zur kompilierende Anwendung keine Referenz hinzu, die die Deklaration von ScriptElements enthält. Du müsstest sozusagen diese Anwendung als Referenz mit eintragen.Grüße Nico
----------------------
Xing
----------------------
Zitat von Mark Twain (1835-1910)
Zitat von Mike Wilson - Biographie über Larry Ellison (CEO Oracle)
-
03.10.08 13:19 #3
- Registriert seit
- Mar 2006
- Beiträge
- 256
Hallo, danke für den Tip, also hier mit geht es jetzt:
PHP-Code:List<string> assemblyNames = new List<string>();
foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
{
if((asm.Location != null) && (asm.Location.Length > 0))
assemblyNames.Add(new Uri(asm.Location).LocalPath);
}
compileUnit.ReferencedAssemblies.AddRange(assemblyNames.ToArray());
-
05.10.08 18:44 #4
- Registriert seit
- Mar 2006
- Beiträge
- 256
Es läuft aber nicht, ich bekomme immer in der Zeile: "runDelegate = (ScriptRun)Delegate.CreateDelegate(typeof(ScriptRun), objFunc, calculationFunction.Name);" Die Exception:
[System.ArgumentException] = {"Fehler beim Binden an die Zielmethode."}Geändert von lordfritte (05.10.08 um 19:44 Uhr)
Ähnliche Themen
-
3ds max scripting
Von holzmcgyver im Forum 3D Studio MaxAntworten: 0Letzter Beitrag: 25.02.10, 11:50 -
3ds max scripting
Von holzmcgyver im Forum 3D Studio MaxAntworten: 0Letzter Beitrag: 25.02.10, 10:37 -
Problem mit C# Scripting
Von lordfritte im Forum .NET Application und Service DesignAntworten: 0Letzter Beitrag: 09.02.08, 22:03 -
InDesign Scripting mit JS
Von JackyD im Forum Desktop Publishing (DTP)Antworten: 2Letzter Beitrag: 10.04.05, 09:57 -
mIRC Scripting...
Von fUnKuCh3n im Forum InternetkommunikationAntworten: 1Letzter Beitrag: 26.07.04, 15:00





Zitieren

Login




