I just get an error each time I run this code:
AppDomain newDomain = AppDomain.CreateDomain("newDomain");
AssemblyName assemblyName = AssemblyName.GetAssemblyName(path);
Command cmd = (Command)newDomain.CreateInstanceAndUnwrap(assemblyName.FullName, typename);
cmd.Execute();
Where path is the path of the Dll and typename is "NWT_Projekt.TestClass"
My command class:
using System;
namespace NWT_Projekt
{
public interface Command
{
void Execute();
}
}
and this is the source code of the DLL
using System;
using System.Collections;
using System.Xml;
using System.IO;
using System.Windows.Forms;
namespace NWT_Projekt
{
public class TestClass : NWT_Projekt.Command
{
public MainForm f;
public TestClass()
{
f = Form.ActiveForm as MainForm;
}
public void Execute()
{
//do something
}
}
}
ERROR (google translator :D)
An exception of type "System.Runtime.Serialization.SerializationException 'occurred in NWT PRojekt.exe.
Additional information: The type "NWT_Projekt.TestClass' in Assembly 'scripts, Version = 0.0.0.0, Culture = neutral, PublicKeyToken = null' is not marked as serializable.
EDIT2:
With the [Serializable] it works now, but after I run the code one time and then I want to create a second dll it gives me an IO error, because the file is in use!
How can I fix this?
If you want your command to run on its own appDomain, you should use MarshalByRefObject.
using System;
using System.Collections;
using System.Xml;
using System.IO;
using System.Windows.Forms;
namespace NWT_Projekt
{
public class TestClass : MarshalByRefObject, NWT_Projekt.Command
{
public MainForm f;
public TestClass()
{
f = Form.ActiveForm as MainForm;
}
public void Execute()
{
//do something
}
}
}
I advice you to prefix your interfaces : ICommand
Related
Ok so im trying to make a program that checks if a program is currently running. It is giving me a error when ever i declare a void. I am new to C# so im sorry if its a stupid.
using System;
using System.Windows;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using Microsoft.VisualBasic.ApplicationServices;
namespace IsProgramRunning
{
private void IsRunning()
{
Process[] pname = Process.GetProcessesByName("VLC Player");
if (pname.Length > 0)
{
MessageBox.Show("Process Running");
}
else
{
MessageBox.Show("Process Not running");
}
System.Threading.Thread.Sleep(5 * 1000);
}
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
ServiceBase.Run(ServicesToRun);
}
}
If im going about this all wrong and there is a easy way to do it in c++ that would be good to
To have instance members and methods, you need a class. You have confused a namespace with a class
namespace MyAwesomeNameSpace
{
public class ProgramRunningHelper
{
// put your class code here
}
}
Compiler Error CS0116
A namespace cannot directly contain members such as fields or methods.
A namespace can contain other namespaces, structs, and classes.
I have C# solution with 2 project:
DLLTest (Console app)
BLib (library)
In DLLTest I set reference to BLib and set Copy Local property to false.
Compile solution.
Copy BLib.dll to 'C:\BLib.dll' and run application.
In first step in my code I load Assembly from path 'C:\BLib.dll' then invoke method from there. On invoking method from BLib assembly fire AssemblyResolver and try load assembly which I loaded before manually.
Can I do something that application to know that the library has already been loaded and not try load it again?
This is BClass.cs file from BLib project:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BLib
{
public class BClass
{
public static void PrintName()
{
Console.WriteLine("BLib");
}
}
}
This is Program.cs file from DLLTest project:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace DLLTest
{
class Program
{
static void Main(string[] args)
{
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
Assembly.LoadFile(#"C:\BLib.dll");
Console.WriteLine("Loaded assembles:");
AppDomain.CurrentDomain.GetAssemblies().ToList()
.ForEach(p => Console.WriteLine(p));
Console.WriteLine("End list of assembles");
try
{
PrintMessage();
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadKey();
}
private static void PrintMessage()
{
BLib.BClass.PrintName();
}
private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
Console.WriteLine(args.Name);
return null;
}
}
}
I find similar question Why is AssemblyResolve called when Assembly is in CurrentDomain? Answer from there follow me to Choosing a Binding Context where everything was detailed described (row Neither column Disadvantages).
That I think so, this is security reason.
I am stuck on what I would think should be a rather basic feature of IronPython integration with C# (this is, of course, a very simplified example). Below is a simple multi-project solution. The first project defines an enum and a class from one namespace
namespace EnumTest
{
public class EnumTest
{
public enum FooEnum
{
FooOne = 101,
FooTwo = 102,
};
public EnumTest(FooEnum f)
{
_f = f;
}
}
}
Then, I have another project which encompasses all of IronPython: the runtime DLLs, the Python modules, and the C# class that runs the python script from a file.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using IronPython.Hosting;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;
namespace IronPythonRunner
{
public class IronPythonRunner
{
public IronPythonRunner()
{
ScriptEngine ironPythonEngine = Python.CreateEngine();
ScriptScope pythonScope = ironPythonEngine.CreateScope();
dynamic scope = pythonScope;
const string script = "c:/temp/try.py";
String scriptDir = Path.GetDirectoryName(script);
String ironPyDir = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "\\IronPythonDistributable\\Lib";
ICollection<String> paths = ironPythonEngine.GetSearchPaths();
paths.Add(scriptDir);
paths.Add(ironPyDir);
ironPythonEngine.SetSearchPaths(paths);
ScriptSource source = ironPythonEngine.CreateScriptSourceFromFile(script);
try
{
source.Execute(pythonScope);
}
catch (Exception e)
{
Debug.WriteLine(e.ToString());
}
finally
{
ironPythonEngine.Runtime.Shutdown();
}
}
}
}
Finally, I have a c# project that is a test GUI for running a python script
using System;
using System.Windows.Forms;
namespace IronPythonNamespaceTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
IronPythonRunner.IronPythonRunner r = new IronPythonRunner.IronPythonRunner();
}
}
}
When I try to run the following python script
print "hello world"
print str(FooEnum.FooOne)
t = EnumTest(FooEnum.FooTwo)
I get the "hello world" output, but then I get a C# IronPython.Runtime.UnboundNameException: name 'FooEnum' is not defined. Which brings me to my question, how should I be accessing the enum and the class from within my python script?
You need to import your assembly:
import clr
clr.AddReference("assembly_name")
from EnumTest import EnumTest
from EnumTest.EnumTest import FooEnum
I don't have enough rep to comment, so I am posting a question here. I read this question Get list of failing tests from Nunit. I am trying to implement the nunit addin, I used this code:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using NUnit.Core.Extensibility;
namespace NunitAddin
{
[NUnitAddinAttribute(Type = ExtensionType.Core,
Name = "addin",
Description = "addin")]
public class NunitAddin : IAddin
{
public bool Install(IExtensionHost host)
{
IExtensionPoint listeners = host.GetExtensionPoint("EventListeners");
if (listeners == null)
return false;
listeners.Install(this);
return true;
}
public void TestStarted(NUnit.Core.TestName testName)
{
}
public void TestFinished(NUnit.Core.TestResult result)
{
}
public void RunStarted(NUnit.Core.TestName testName)
{
}
public void RunFinished(NUnit.Core.TestResult result)
{
}
public void UnhandledException(Exception exception)
{
}
public void TestOutput(NUnit.Core.TestOutput testOutput)
{
}
}
}
But when I call it using
var addin = new NunitAddin.NunitAddin();
var a = addin.Install(CoreExtensions.Host);
I get an error
NunitAddin.NunitAddin is not {0} extension point
on
listeners.Install(this);
Does anyone know how to solve this problem?
Never mind, issue solved. Just a stupid mistake, I had NunitAddin : IAddin instead of NunitAddin : IAddin; EventListener
I have two classes in class library
namespace ClassLibrary3
{
public class Class1
{
public string title;
public string author;
public Class1(string title, string author)
{
this.title = title;
this.author = author;
}
}
}
Another class
using System;
using System.Collections;
using System.Linq;
using System.Text;
namespace ClassLibrary3
{
class Class2
{
private Hashtable books;
public Class2()
{
books = new Hashtable();
}
public void addBook(Class1 book)
{
books.Add(book.title, book);
}
public Class1 getBook(String title, String author)
{
return (Class1)books[title];
}
public void removeBook(string title)
{
if (books[title] != null)
books.Remove(title);
}
}
}
And my test is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using System.Collections;
namespace ClassLibrary3
{
[TestFixture]
class TEST
{
[Test]
public void getbooktest()
{
Class1 c1 = new Class1("story", "James");
Class2 c2 = new Class2();
Assert.AreEqual("story", c2.getBook("story", "James"));
}
}
}
Basicly the problem is Nunit doesnt test it. It finds the dll. Loads the test class. But dont come upto the test method.
Please any idea..........
NUnit can't see your TEST class unless you mark it as public, change it to
[TestFixture]
public class TEST
{
...
Side note, consider giving it a better name than TEST ;-)