Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

help on this?

jeffmanjeffman Member Posts: 112
the sample code below i need help with i converted it to visual basic .net code, but it needs some corrections becuase iam not getting the results i wanted. i probably just need to format the lcCode to variable correctly and add vbcrlf(i think vbcrlf is the same as
for a new line i c++).
can anyone convert this code to visual basic .net 2005 and tell me the fully functional working code or tell me what errors i need to fix to get it to work.
using System.Reflection;
using System.CodeDom.Compiler;
using Microsoft.CSharp; // and/or Microsoft.VisualBasic;
private void button1_Click(object sender, System.EventArgs e)
{
// Example form input has code in a text box
string lcCode = this.txtCode.Text;
'you dont need in visual basic .net 2005 to make a new vbcodeprovider.createcompiler like below i think anyways
ICodeCompiler loCompiler = new CSharpCodeProvider().CreateCompiler();
' the rest stays the same with some difference when changing to visual basic . net code
CompilerParameters loParameters = new CompilerParameters();
// Start by adding any referenced assemblies
loParameters.ReferencedAssemblies.Add("System.dll" );
loParameters.ReferencedAssemblies.Add(
"System.Windows.Forms.dll");
// Must create a fully functional assembly as a string
lcCode = @using System;
using System.IO;
using System.Windows.Forms;
namespace MyNamespace {
public class MyClass {
public object DynamicCode(params object[] Parameters) {
" + lcCode +
"} } }";
// Load the resulting assembly into memory
loParameters.GenerateInMemory = false;
// Now compile the whole thing
CompilerResults loCompiled =
loCompiler.CompileAssemblyFromSource(loParameters, lcCode);
if (loCompiled.Errors.HasErrors) {
string lcErrorMsg = "";
lcErrorMsg = loCompiled.Errors.Count.ToString() +
" Errors:";
for (int x=0;x<loCompiled.Errors.Count;x++)
lcErrorMsg = lcErrorMsg + "
Line: " +
loCompiled.Errors[x].Line.ToString() +
" - " + loCompiled.Errors[x].ErrorText;
MessageBox.Show(lcErrorMsg + "

" + lcCode,
"Compiler Demo");
return;
}
Assembly loAssembly = loCompiled.CompiledAssembly;
// Retrieve an obj ref - generic type only
object loObject =
loAssembly.CreateInstance("MyNamespace.MyClass");
if (loObject == null) {
MessageBox.Show("Couldn't load class.");
return;
}
object[] loCodeParms = new object[1];
loCodeParms[0] = "West Wind Technologies";
try {
object loResult = loObject.GetType().InvokeMember(
"DynamicCode",BindingFlags.InvokeMethod,
null,loObject,loCodeParms);
DateTime ltNow = (DateTime) loResult;
MessageBox.Show("Method Call Result:

" +
loResult.ToString(),"Compiler Demo");
}
catch(Exception loError) {
MessageBox.Show(loError.Message,"Compiler Demo"); }
}
Sign In or Register to comment.