I wish to invoke code that is in selected txt file. It works fine until file content is something simple like "Text string". It also works if i pass string parameter into it. But when i try passing an object like in my case Global it fails. Error is: "The type or namespace name 'Global' could not be found (are you missing a using directive or an assembly reference?)"
Here is some code..
private void button1_Click(object sender, EventArgs e)
{
Scripting scriptObj = new Scripting();
scriptObj.fileName = this.openFileDialog1.FileName;
scriptObj.tekst = File.ReadAllText(this.openFileDialog1.FileName);
string exit = scriptObj.GetAction();
this.label1.Text = exit;
}
namespace WindowsFormsApplication2
{
public class Global
{
public string fileName = "test string";
}
public class Scripting
{
public string tekst = "";
public string fileName = "";
public string MyMethod1(Global obj) { return (obj.fileName); }
public string GetAction()
{
string sourceCode = #" namespace WindowsFormsApplication2 { public class Scripting { public string MyMethod (Global obj) { return (" + tekst + "); }}}";
var compParms = new CompilerParameters
{
GenerateExecutable = false,
GenerateInMemory = true
};
var csProvider = new CSharpCodeProvider();
CompilerResults compilerResults = csProvider.CompileAssemblyFromSource(compParms, sourceCode);
if (compilerResults.Errors.HasErrors)
{
StringBuilder errors = new StringBuilder("Compiler Errors :\r\n");
foreach (CompilerError error in compilerResults.Errors)
{
errors.AppendFormat("Line {0},{1}\t: {2}\n",
error.Line, error.Column, error.ErrorText);
}
return errors.ToString();
}
else
{
Global newGlobal = new Global();
newGlobal.fileName = "TEsTfileNameToOutput";
object typeInstance = compilerResults.CompiledAssembly.CreateInstance("WindowsFormsApplication2.Scripting");
MethodInfo mi = typeInstance.GetType().GetMethod("MyMethod");
string methodOutput = (string)mi.Invoke(typeInstance, new object[]{ newGlobal });
return methodOutput;
}
}
}
}
Why does
public string MyMethod (Global obj) { return (" + tekst + "); }
not take Global as param, but it works ok with MyMethod1
public string MyMethod1(Global obj) { return (obj.fileName); }
Content of selected file is: obj.fileName
You haven't included a reference to the current assembly, which is the assembly declaring Global. Therefore the compiler has no idea which type you're talking about.
You need to set the ReferencedAssemblies property in the CompilerParameters that you're creating.
Related
I have been running into a rather frustrating issue. I am attempting to authenticate a user against an Active Directory, and in order to do so I pass my users variables into the following class.
public static ILdapAuthentication CreateInstance(string domainAndUser, string password, string ldapPath)
{
string[] dllPaths = Directory.GetFiles(ExecutingAssemblyDirectory, "*.dll");
List<Assembly> listOfAssemblies = new List<Assembly>();
foreach (var dllPath in dllPaths.Where(x => x.Contains("ActiveDirectoryAuthentication")))
{
Assembly assembly = Assembly.LoadFrom(dllPath);
listOfAssemblies.Add(assembly);
}
Type type = null;
int foundTypes = 0;
foreach (var assembly in listOfAssemblies)
{
type =
assembly.GetTypes()
.FirstOrDefault(x => x.GetInterfaces().Any(i => i == typeof(ILdapAuthentication)));
if (type == null)
continue;
foundTypes++;
}
if (foundTypes == 0)
throw new Exception("ActiveDirectoryAuthentication DLL not found.");
if (foundTypes > 1)
throw new Exception("Only one ActiveDirectoryAuthentication DLL must be used.");
return Activator.CreateInstance(type, domainAndUser, password, ldapPath) as ILdapAuthentication;
}
The issue occurs in the foreach loop, as I attempt to get my Types, it always returns null, and doesn't even hit the Interface (ILDPAuthentication) code below.
public interface ILdapAuthentication
{
bool IsActiveDirectoryUserValid();
}
which invokes the following code:
public class LdapAuthentication : ILdapAuthentication
{
private string DomainAndUser { get; set; }
private string Password { get; set; }
private string LdapPath { get; set; }
public LdapAuthentication(string domainAndUser, string password, string ldapPath)
{
this.DomainAndUser = domainAndUser;
this.Password = password;
this.LdapPath = ldapPath;
}
public bool IsActiveDirectoryUserValid()
{
try
{
if (!this.DomainAndUser.Contains('\\'))
throw new Exception("Domain User is invalid.");
string[] userLogin = this.DomainAndUser.Split('\\');
string domain = userLogin[0];
string userName = userLogin[1];
DirectoryEntry entry = new DirectoryEntry(this.LdapPath, this.DomainAndUser, this.Password);
object obj = entry.NativeObject;
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(SAMAccountName=" + userName + ")";
search.PropertiesToLoad.Add("CN");
SearchResult result = search.FindOne();
if (null == result)
{
return false;
}
else
{
return true;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
throw;
}
}
}
The initial class looks for the DLLs in my application folder called ActiveDirectoryAuthentication which I have copied in.
I have seen this before - types in a explicitly loaded assembly do not match types in a referenced project. Because you are using:
i == typeof(ILdapAuthentication)
you are reliant on the equality comparison for the Type class, which may not return equality when you are expecting it to. I suggest instead you do:
i.FullName == typeof(ILdapAuthentication).FullName
which will use a simple string comparison.
Just looking for the solution where I want to add the c# script to the cache after compilation so that it can be accessed at any time when required.
Class which holds the cache details.
Public Class cCache
{
public string sSessionID{get;set;}
public string sName{get;set;}
public string sValue{get;set;}
}
C# script code used:
public static Run(string sName)
{
"Console.WriteLine(sName);"
}
When the button is clicked the method is called in the repository where it calls the another method with the script as the parameter to be complied and returns back the result in "MethodInfo" reflection. The script in the method info is invoked with parameter for the script and executed.
Code:
public string Button_CLick(string sScript)
{
MethodInfo methodInfo = WriteMethod(sScript);
cCache cChe= new cCache();
cChe.sSessionID="XYZ";
cChe.sName="Script";
cChe.sValue=MethodInfo.ToString();
if (methodInfo != null)
{
oResult = methodInfo.Invoke(null, new object[] { sName });
}
}
public MethodInfo WriteMethod(string sScript)
{
string sCode= #" using System;
namespace Scripting
{
public class AddScript
{
" + sScript + #"
}
}
";
CompilerParameters loParameters = new CompilerParameters();
loParameters.ReferencedAssemblies.Add("System.dll");
loParameters.GenerateInMemory = false;
ICodeCompiler provider = new CSharpCodeProvider().CreateCompiler();
CompilerResults results = provider.CompileAssemblyFromSource(loParameters, sCode);
if (results.Errors.HasErrors)
{
StringBuilder sb = new StringBuilder();
foreach (CompilerError error in results.Errors)
{
sb.AppendLine(String.Format("Error ({0}): {1}", error.ErrorNumber, error.ErrorText));
}
throw new InvalidOperationException(sb.ToString());
}
Type binaryFunction = results.CompiledAssembly.GetType("Scripting.AddScript");
return binaryFunction.GetMethod(Run);
}
The above code is working fine but before invoking the script method I want to add the complied script to the cache and then invoke or call it with parameter when ever required as the class property is string I am getting error when converting it to string, so please anyone can help me to sort this error.
i have working CompileAssemblyFromSource code. But when i use any code protector like RedGate SmartAssembly or Themida it's stop working and i get error "Could not load file or assembly or one of its dependencies". Can you please help me with that?
using System;
using System.CodeDom.Compiler;
using System.Reflection;
using Microsoft.CSharp;
namespace stringToCode
{
public class Program
{
public static int q = 0;
static void Main(string[] args)
{
try
{
string source = "namespace stringToCode { public class FooClass { public void Execute() { Program.q = 1; } } }";
Console.WriteLine("q=" + q);
using (var foo = new CSharpCodeProvider())
{
var parameters = new CompilerParameters();
parameters.GenerateInMemory = true;
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
{
try
{
string location = assembly.Location;
if (!String.IsNullOrEmpty(location))
{
parameters.ReferencedAssemblies.Add(location);
}
}
catch (NotSupportedException)
{ }
}
var res = foo.CompileAssemblyFromSource(parameters, source);
var type = res.CompiledAssembly.GetType("stringToCode.FooClass");
var obj = Activator.CreateInstance(type);
var output = type.GetMethod("Execute").Invoke(obj, new object[] { });
Console.WriteLine("q=" + q);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);;
}
Console.ReadLine();
}
}
}
sorry for my question. I just understand why it happens)
This is code is example. The main problem i have with code that i get from server.
So when i obfuscate my vars i don't obfuscare them at my "online" code that i use with CompileAssemblyFromSource. So this just can't work. Because vars don't have same names.
i try to use CompileAssemblyFromSource to change 1 value at my main class.
But when i compile i get error "Could not load file or assembly or one of its dependencies" and this only happens when i try change static value of other class. But if i return some output or wrote anything at Console from this FooClass than all work's fine. But how can i change value of other class?
using System;
using System.CodeDom.Compiler;
using System.Reflection;
using Microsoft.CSharp;
namespace stringToCode
{
class Program
{
public static int q = 0;
static void Main(string[] args)
{
string source = "namespace stringToCode { public class FooClass { public void Execute() { Program.q = 1; } } }";
Console.WriteLine("q=" + q);
using (var foo = new CSharpCodeProvider())
{
var parameters = new CompilerParameters();
parameters.GenerateInMemory = true;
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
{
try
{
string location = assembly.Location;
if (!String.IsNullOrEmpty(location))
{
parameters.ReferencedAssemblies.Add(location);
}
}
catch (NotSupportedException)
{}
}
var res = foo.CompileAssemblyFromSource(parameters ,source);
var type = res.CompiledAssembly.GetType("FooClass"); //<- here i has error
var obj = Activator.CreateInstance(type);
var output = type.GetMethod("Execute").Invoke(obj, new object[] { });
Console.WriteLine("q=" + q);
Console.ReadLine();
}
}
}
}
You can't find the type because you have compilation error in your code.You can't access the classes in your current code in this manner. You should at least reference the current assembly in your in-memory assembly.
UPDATE
You have two issues in your code. First, you have to make the class Program public. Then you should specify the full name of type in GetType method.
This code works fine:
using System;
using System.CodeDom.Compiler;
using System.Reflection;
using Microsoft.CSharp;
namespace stringToCode
{
public class Program
{
public static int q = 0;
static void Main(string[] args)
{
string source = "namespace stringToCode { public class FooClass { public void Execute() { Program.q = 1; } } }";
Console.WriteLine("q=" + q);
using (var foo = new CSharpCodeProvider())
{
var parameters = new CompilerParameters();
parameters.GenerateInMemory = true;
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
{
try
{
string location = assembly.Location;
if (!String.IsNullOrEmpty(location))
{
parameters.ReferencedAssemblies.Add(location);
}
}
catch (NotSupportedException)
{}
}
var res = foo.CompileAssemblyFromSource(parameters ,source);
var type = res.CompiledAssembly.GetType("stringToCode.FooClass"); //<- here i has error
var obj = Activator.CreateInstance(type);
var output = type.GetMethod("Execute").Invoke(obj, new object[] { });
Console.WriteLine("q=" + q);
Console.ReadLine();
}
}
}
}
I compile a plugin and then create an instance of it in a new AppDomain. Then I want to execute a method in the plugin object but that results in an ArgumentException:
"Object of type System.MarshalByRefObject can not be converted to type Data"
The code below should be compilable and result in the exception above. To make it work you must also sign the Assembly (Project -> Properties -> Signing -> Sign the assembly -> New ...)
IPlugin.cs:
using System;
namespace Plugin
{
[Serializable]
class Data : MarshalByRefObject
{
int value;
}
interface IPlugin
{
void DoStuff(Data data);
}
}
Program.cs:
using System;
using System.CodeDom.Compiler;
using System.IO;
using System.Security.Policy;
using System.Security;
using System.Security.Permissions;
using System.Reflection;
namespace Plugin
{
class Program
{
public class Sandbox : MarshalByRefObject
{
const string BaseDirectory = "Untrusted";
const string DomainName = "Sandbox";
private object instance;
public static Sandbox Create()
{
var setup = new AppDomainSetup()
{
ApplicationBase = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, BaseDirectory),
ApplicationName = DomainName,
DisallowBindingRedirects = true,
DisallowCodeDownload = true,
DisallowPublisherPolicy = true
};
Evidence ev = new Evidence();
ev.AddHostEvidence(new Zone(SecurityZone.Internet));
var permissions = new PermissionSet(PermissionState.None);
permissions.AddPermission(new ReflectionPermission(ReflectionPermissionFlag.RestrictedMemberAccess));
permissions.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution));
StrongName fullTrustAssembly = typeof(Sandbox).Assembly.Evidence.GetHostEvidence<StrongName>();
var domain = AppDomain.CreateDomain(DomainName, null, setup, permissions, fullTrustAssembly);
return (Sandbox)Activator.CreateInstanceFrom(domain, typeof(Sandbox).Assembly.ManifestModule.FullyQualifiedName, typeof(Sandbox).FullName).Unwrap();
}
public bool CreateInstance(string assemblyPath, string scriptType)
{
new FileIOPermission(FileIOPermissionAccess.Read | FileIOPermissionAccess.PathDiscovery, assemblyPath).Assert();
var assembly = Assembly.LoadFrom(assemblyPath);
CodeAccessPermission.RevertAssert();
Type type = assembly.GetType(scriptType);
if (type == null)
return false;
instance = Activator.CreateInstance(type);
return instance != null;
}
public object Execute(string method, params object[] parameters)
{
Type type = instance.GetType();
MethodInfo info = type.GetMethod(method);
return info.Invoke(instance, parameters);
}
}
private static void CompileToFile(string code, string ifaceFile, string output)
{
CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
CompilerParameters p = new CompilerParameters();
p.GenerateInMemory = false;
p.GenerateExecutable = false;
p.OutputAssembly = output;
provider.CompileAssemblyFromSource(p, code, ifaceFile);
}
private static string code = #"
using Plugin;
class MyPlugin : IPlugin
{
public void DoStuff(Data data)
{
}
}
";
static void Main(string[] args)
{
string iface = System.IO.File.ReadAllText(#"C:\Documents and Settings\markus.BLUE\mina dokument\visual studio 2010\Projects\Plugin\Plugin\IPlugin.cs");
string pluginObjectFile = System.IO.Path.GetTempFileName();
CompileToFile(code, iface, pluginObjectFile);
Sandbox s = Sandbox.Create();
s.CreateInstance(pluginObjectFile, "MyPlugin");
Data data = new Data();
s.Execute("DoStuff", data);
}
}
}
It must be a type known at both sides of your communication!