I'm trying to dynamically load a class, from a dll, at run time then cast it as in interface.
I have a class define in a dll called MyClass.dll like below.
namespace MyClass
{
public class Class1 : IMyInterface
{
public int MyNumber { get; set; } = 6;
public int GetMyNumber()
{
return MyNumber;
}
}
}
Then, i have an interface defined in a shared dll like below:
namespace Common
{
public interface IMyInterface
{
int MyNumber { get; set; }
int GetMyNumber();
}
}
Finally, i have my code attempted to load the assembly and assign it as a IMyInterface.
private void Form1_Load(object sender, EventArgs e)
{
string fName = #"D:\Development\Research\Dynamic Load\MyClass\bin\Debug\MyClass.dll";
Assembly decoupledAssembly = Assembly.LoadFrom(fName);
if (decoupledAssembly != null) //All Good
{
Type t = decoupledAssembly.GetType("MyClass.Class1");
//Good here too, it finds it just fine.
IMyInterface mi = (Activator.CreateInstance(t) as IMyInterface);
//Now i'm screwed screwed.
if (mi != null)
MessageBox.Show(mi.GetMyNumber().ToString());
}
}
You can see in the comments where it is breaking. I find the type, no problem, but returns null when I attempted to cast it as my interface.
Thanks in advance for your help.
You have to reference the full path to the interface. Strange, but once I did this it started working. See the changes below.
namespace MyClass
{
public class Class1 : Common.IMyInterface
{
public int MyNumber { get; set; } = 6;
public int GetMyNumber()
{
return MyNumber;
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
string fName = #".....\MyClass.dll";
Assembly decoupledAssembly = Assembly.LoadFrom(fName);
if (decoupledAssembly != null) //All Good
{
Type t = decoupledAssembly.GetType("MyClass.Class1");
//Good here too, it finds it just fine.
Common.IMyInterface mi = (Activator.CreateInstance(t) as Common.IMyInterface);
//Now it works.
if (mi != null)
MessageBox.Show(mi.GetMyNumber().ToString());
}
}
Related
I have an industrial computer with some Digital I/O pins. The manufacturer provides some C++ libraries and examples to handle pin status change.
I need to integrate this events onto a C# application. AFAIK the most simple way to perform this is:
Make a managed C++/CLI wrapper for the manufacturer libraries that fires events when interruptions are issued from the DIO pins.
Reference that wrapper and handle the events in the C# part as it they were normal C# events.
I have tried to make this work with some mock objects with no luck. From the docs, the function EventHandler should do most of the "dirty work" in my case. Following info available in old threads and the EventHandler example in the MSDN docs I ended up with this test code:
C++/CLI
using namespace System;
public ref class ThresholdReachedEventArgs : public EventArgs
{
public:
property int Threshold;
property DateTime TimeReached;
};
public ref class CppCounter
{
private:
int threshold;
int total;
public:
CppCounter() {};
CppCounter(int passedThreshold)
{
threshold = passedThreshold;
}
void Add(int x)
{
total += x;
if (total >= threshold) {
ThresholdReachedEventArgs^ args = gcnew ThresholdReachedEventArgs();
args->Threshold = threshold;
args->TimeReached = DateTime::Now;
OnThresholdReached(args);
}
}
event EventHandler<ThresholdReachedEventArgs^>^ ThresholdReached;
protected:
virtual void OnThresholdReached(ThresholdReachedEventArgs^ e)
{
ThresholdReached(this, e);
}
};
public ref class SampleHandler
{
public:
static void c_ThresholdReached(Object^ sender, ThresholdReachedEventArgs^ e)
{
Console::WriteLine("The threshold of {0} was reached at {1}.",
e->Threshold, e->TimeReached);
Environment::Exit(0);
}
};
void main()
{
return;
CppCounter^ c = gcnew CppCounter(20);
c->ThresholdReached += gcnew EventHandler<ThresholdReachedEventArgs^>(SampleHandler::c_ThresholdReached);
Console::WriteLine("press 'a' key to increase total");
while (Console::ReadKey(true).KeyChar == 'a') {
Console::WriteLine("adding one");
c->Add(1);
}
}
C#
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
CppCounter cc = new CppCounter(5);
//cc.ThresholdReached += cs_ThresholdReached; //<--This is the offending line
Console.WriteLine("press 'a' key to increase total");
while (Console.ReadKey(true).KeyChar == 'a')
{
Console.WriteLine("adding one");
cc.Add(1);
}
}
static void cs_ThresholdReached(object sender, ThresholdReachedEventArgs e)
{
Console.WriteLine("The threshold of {0} was reached at {1}.", e.Threshold, e.TimeReached);
Environment.Exit(0);
}
}
class Counter
{
private int threshold;
private int total;
public Counter(int passedThreshold)
{
threshold = passedThreshold;
}
public void Add(int x)
{
total += x;
if (total >= threshold)
{
ThresholdReachedEventArgs args = new ThresholdReachedEventArgs();
args.Threshold = threshold;
args.TimeReached = DateTime.Now;
OnThresholdReached(args);
}
}
protected virtual void OnThresholdReached(ThresholdReachedEventArgs e)
{
EventHandler<ThresholdReachedEventArgs> handler = ThresholdReached;
if (handler != null)
{
handler(this, e);
}
}
public event EventHandler<ThresholdReachedEventArgs> ThresholdReached;
}
public class ThresholdReachedEventArgs : EventArgs
{
public int Threshold { get; set; }
public DateTime TimeReached { get; set; }
}
}
What am I doing wrong? Is it something I am missing?
public class ThresholdReachedEventArgs : EventArgs
The code is correct, except for this minor glitch. You accidentally re-declared this class in your C# code. Now there are two, one from your C++/CLI project and another from your C# project. That is a problem, type identity in .NET is not just determined by the namespace name and class name, it also includes the assembly it came from.
So these are two distinct types, the compiler tries to tell you that the C# version of it is not the correct one. That they have the same name doesn't exactly help you decode the error message :)
Very easy to fix, simply delete the class declaration from your C# code. Now the compiler will use the C++/CLI version of it.
I know that what i need help with is not called the Parent but that was the closest word i could think of to describe this situation.
This is not actual code i'm using.
I have MainClass which contains object ObjectA.
class MainClass
{
public int mode = 0;
ObjectA obj = new ObjectA();
}
I need to access the mode variable from the ObjectA object. Is this possible? I feel like if i could at least call a method in MainClass from obj i would be all set. Also I'm aware calling MainClass a parent in this sense is incorrect, what is the correct term for both the MainClass and obj in this.
Assuming you are able to change MainClass, I can think of two options:
Add a reference to MainClass when you create an ObjectA instance.
Like you said in your comments, use events.
Using a reference:
class MainClass
{
public int mode = 31416;
ObjectA obj;
public MainClass()
{
obj = new ObjectA(this);
}
public int GetMainClassMode()
{
return mode;
}
public void Test() {
Console.WriteLine("Calling test method inside obj");
obj.Test();
}
}
class ObjectA {
MainClass parent = null;
public ObjectA(MainClass parent)
{
this.parent = parent;
}
public void Test()
{
if (parent != null)
{
Console.WriteLine("Getting mode from 'parent' MainClass");
Console.WriteLine(string.Format("Mode = {0}", parent.GetMainClassMode()));
}
}
}
Using events:
class MainClass
{
public int mode = 31416;
ObjectA obj = new ObjectA();
public MainClass()
{
obj.ValueReturnEvent += HandleValueReturnEvent;
}
public int GetMainClassMode()
{
return mode;
}
// Handle event, return data
private int HandleValueReturnEvent(object sender, EventArgs e)
{
return mode;
}
public void Test() {
Console.WriteLine("Calling test method inside obj");
obj.Test();
}
}
class ObjectA {
// delegate
public delegate int ReturnValueEventHandler(object sender, EventArgs args);
// event
public event ReturnValueEventHandler ValueReturnEvent;
public void Test()
{
// make sure at least one subscriber
if (ValueReturnEvent != null)
{
// note the event is returning a value
var myValue = ValueReturnEvent(this, null);
Console.WriteLine("Getting mode from 'parent' MainClass");
Console.WriteLine(string.Format("Mode = {0}", myValue));
}
}
}
On both cases, you get this output:
Calling test method inside obj
Getting mode from 'parent' MainClass
Mode = 31416
Even though you've got your answer, you could also simply inject it into your ObjectA ... have a constructor that takes an int, and when you create the object pass the mode in and save it in that object.
I find it cleaner that the object uses whatever it needs from its scope, rather than accessing the parent to ask for a variable.
I'm not saying it might not be needed, it just another thought.
You'll have to wire things up to let the child know about the parent for this to work. Something like this:
class ParentClass
{
public int mode = 0;
public ChildClass child = null;
public ParentClass()
{
child = new ChildClass(this);
}
}
class ChildClass
{
public readonly ParentClass parent = null;
public ChildClass (ParentClass parent)
{
this.parent = parent;
}
public int MethodThatReadsParentMode()
{
int mode = parent.mode;
return mode;
}
}
You make field mode public, so I assume it's possible that someone who uses class MainClass do can change this field.
Let's assume program looks like this.
class Program {
var main = new MainClass();
main.mode = 1;
}
Your obj field is private, so Program cannot access it.
So, when someone changed field mode, ObjectA should get new value of field mode.
Possible solution:
class ObjectA {
public int Mode { get; set; }
}
class MainClass {
private obj = new ObjectA();
public int Mode {
get { return this.obj.Mode; }
set { this.obj.Mode = value; }
}
}
There is another option in case field mode belongs to MainCalss.
class ObjectA {
private int mode;
public ObjectA(int mode) {
this.mode = mode;
}
// you can create property instead of method
// I'm not sure how you use this variable, so I just added set method
public void SetMode(int mode) {
this.mode = mode;
}
}
class MainClass {
private int mode = 0;
private obj = new ObjectA();
public int Mode {
get { return this.mode; }
set {
this.obj.SetMode(value);
this.mode = value;
}
}
}
I have a form that has a button to get a method executed in another class.
Code on the form:
public delegate void CustomPreviewCreate();
public static event CustomPreviewCreate CustomPreviewCreate_Do;
private void CreatePreview()
{
if (CustomPreviewCreate_Do !=null)
{
CustomPreviewCreate_Do();
}
}
This event then gets handled in another class. What I would like to achieve is that I can feed back to the form some form of return value if the method correctly executed.
What I tried so far does not get me the result.
Here is the code:
public void Initialize()
{
SubAsstViewPartControl.CustomPreviewCreate_Do += SubAsstViewPartControl_CustomPreviewCreate_Do;
// this gives me a the compiler error that the return type is wrong
}
private bool SubAsstViewPartControl_CustomPreviewCreate_Do()
{
// do stuff
return false;
}
Is there any direct way to return value from an event handler or I need to use a separate static field to store the event result in?
Update:
Per #Jon's comment, which seemed the simplest to me, I added an answer below demonstrating the simplest approach.
The common approach is to encapsulate your value in the type of EventArgs your event expects. For example, the Framework's CancelEventArgs contains a settable bool Cancel property, allowing each CancelEventHandler to assign a value. The sender can then read the property after the event has been invoked. You could also use a container-like EventArgs class if you want to collect separate values from individual event handlers. For example:
using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
public class SingleValueEventArgs : EventArgs
{
public int Value { get; set; }
}
public class MultiValueEventArgs : EventArgs
{
private List<int> _values = new List<int>(); // Private to prevent handlers from messing with each others' values
public IEnumerable<int> Values
{
get { return _values; }
}
public void AddValue(int value) { _values.Add(value); }
}
public class Exposer
{
public event EventHandler<SingleValueEventArgs> WantSingleValue;
public event EventHandler<MultiValueEventArgs> WantMultipleValues;
public void Run()
{
if (WantSingleValue != null)
{
var args = new SingleValueEventArgs();
WantSingleValue(this, args);
Console.WriteLine("Last handler produced " + args.Value.ToString());
}
if (WantMultipleValues != null)
{
var args = new MultiValueEventArgs();
WantMultipleValues(this, args);
foreach (var value in args.Values)
{
Console.WriteLine("A handler produced " + value.ToString());
}
}
}
}
public class Handler
{
private int _value;
public Handler(Exposer exposer, int value)
{
_value = value;
exposer.WantSingleValue += exposer_WantSingleValue;
exposer.WantMultipleValues += exposer_WantMultipleValues;
}
void exposer_WantSingleValue(object sender, SingleValueEventArgs e)
{
Console.WriteLine("Handler assigning " + _value.ToString());
e.Value = _value;
}
void exposer_WantMultipleValues(object sender, MultiValueEventArgs e)
{
Console.WriteLine("Handler adding " + _value.ToString());
e.AddValue(_value);
}
}
class Program
{
static void Main(string[] args)
{
var exposer = new Exposer();
for (var i = 0; i < 5; i++)
{
new Handler(exposer, i);
}
exposer.Run();
}
}
}
Per Jon Skeet's comment, which seemed the simplest to me, the simplest approach seems to be as follows:
public delegate bool CustomPreviewCreate(); // here we declare a return type
public static event CustomPreviewCreate CustomPreviewCreate_Do;
private void CreatePreview()
{
if (CustomPreviewCreate_Do !=null)
{
bool returnval = CustomPreviewCreate_Do();
}
}
And then:
// the method is declared to return the same type
bool SubAsstViewPartControl_CustomPreviewCreate_Do()
{
// do stuff
return true; // return the value of the type declared
}
I am trying to create an event inside my class and handle it from static void main method.my event is triggered by a method named checkAge().But i have got an error like this :
Error1-An object reference is required for the non-static field,
method, or property 'Event.Program.m_AgeChecker(int)
I think i did all prats that i had to do,& i don't know what is the problem.
Code of my first class
class Mahmud
{
public Mahmud()
{
name = "mahmud";
age = 25;
}
private string name;
private int age;
public string Name
{
get{return name;}
set{name=value;}
}
public int Age
{
get { return age; }
set { age = value; }
}
public void checkAge()
{
AgeUpdate(age);
}
public delegate void AgeEventHandler(int mAge);
public event AgeEventHandler AgeUpdate;
}
Code of the second class
static void Main(string[] args)
{
Mahmud m = new Mahmud();
m.AgeUpdate += new Event.Mahmud.AgeEventHandler(m_AgeChecker(m.Age));
m.Age = 16;
m.checkAge();
m.Age = 27;
m.checkAge();
}
private void m_AgeChecker(int A)
{
if (A > 25)
{
Console.WriteLine("!");
}
else
{
Console.WriteLine("ok");
}
}
It looks like you are calling a non-static method from a static method. You will need to make the method static.
static void m_AgeChecker(int A)
{
if (A > 25)
{
Console.WriteLine("!");
}
else
{
Console.WriteLine("ok");
}
}
I'm unsure as to what is the best approach for passing events down the line to parent classes and in need of some feedback.
The example code below tries to illustrate what I want to achieve.
namespace test {
public delegate void TestCompletedEventHandler(object sender,
TestCompletedEventArgs e);
public class Manager {
CarList m_carlist = null;
public CarList Cars {
get { return m_carlist; }
set { m_carlist = value; }
}
public Manager() {
Cars = new CarList(this);
}
public void Report(bool successfull) {
//...
}
}
public class CarList : List<Car> {
protected internal event TestCompletedEventHandler
Car_TestCompleted = null;
protected readonly Manager m_manager = null;
public Manager Manager {
get { return m_manager; }
}
public CarList(Manager manager) {
m_manager = manager;
}
public void Test() {
foreach(Car car in this) {
bool ret = car.Test();
manager.Report(ret);
}
}
public void Add(Car car) {
//Is this a good approach?
car.TestCompleted +=
new TestCompletedEventHandler(Car_TestCompleted_Method);
base.Add(car);
}
private void Car_TestCompleted_Method(object sender,
TestCompletedEventArgs e)
{
if(Car_TestCompleted != null) Car_TestCompleted(sender, e);
}
}
public class Car {
protected internal event TestCompletedEventHandler
TestCompleted = null;
public bool Test() {
//...
if(TestCompleted != null) TestCompleted(this,
new TestCompletedEventArgs())
}
}
public class TestCompletedEventArgs : EventArgs {
//...
}
}
using test;
Manager manager = new Manager();
manager.Cars.Car_TestCompleted +=
new TestCompletedEventHandler (Car_TestCompleted_Method);
manager.Cars.Test();
Another more specific example:
//Contains DataItems and interfaces for working with them
class DataList
{
public List<DataItem> m_dataitems { get; set; }
public TestManager m_testmanager { get; set; }
// ...
}
class DataItem
{
// ...
}
//A manager class for running tests on a DataList
class TestManager
{
public List<TestSource> m_sources { get; set; }
public WorkerManager m_workermanager { get; set; }
// ...
}
//A common interface for Tests
abstract class TestSource
{
public event EventHandler<EventArgs<object>> Completed = null;
protected TestManager m_owner { get; set; }
public abstract void RunAsync();
// ...
}
//A test
class Test1 : TestSource
{
public virtual void RunAsync()
{
//Add commands
//Run workers
//Report progress to DataList and other listeners (like UI)
//Events seem like a bad approach since they need to be forwarded through many levels of abstraction
if(Completed != null) Completed(this, new EventArgs<object>(null));
}
// ...
}
//Manages a number of workers and a queue of commands
class WorkerManager
{
public List<MyWorker> m_workers { get; set; }
public Queue<Command> m_commands { get; set; }
}
//Wrapper for BackgroundWorker
class MyWorker
{
// ...
}
//Async command
interface Command
{
// ...
}
I think you may have just over implemented this a bit... It looks like you are trying to use async operations. Even if you are using sync operations though, typically you'd just use callback methods instead of events in a case like this...
Here is an example of things to change to use callbacks here:
//new delegate
public delegate void CarReportCallback(Car theCar, bool result);
//in the Manager class, make report conform to delegate's signature
public void Report(Car theCar, bool result)
{
//do something, you know which car and what the result is.
}
//in the CarList class pass a reference to the report method in
public void Test()
{
foreach(Car car in this)
{
car.Test(manager.Report);
}
}
//in the Car class use the delegate passed to invoke the reporting
public void Test(CarReportCallback callback)
{
//... do stuff
callback(this, isTestCompleted);
}
It seems reasonable, but I'm not really sure what the use case is and how this would be used.
You've got a strong concept of containment going on, but I'm not really sure why. Also, it's kind of weird that the CarList 'sort of' seems to have ownership of the individual cars.
Additionally, I don't know why Test() on the Car class would both return a result and raise an event. It seems like you're having two different paths to return the same data. And the Manager class seems completely redundant with the CarList class at first glance.
What is the problem you're actually trying to solve here? That might help me with defining a good solution to it.
It wouldn't make sense to just have each car call an event which calls an event on the parent list. I would do it more like this:
namespace test {
public delegate void TestCompletedEventHandler(object sender,
TestCompletedEventArgs e);
public class Manager {
CarList m_carlist = null;
public CarList Cars {
get { return m_carlist; }
set { m_carlist = value; }
}
public Manager() {
Cars = new CarList(this);
}
public void Report(bool successful) {
//...
}
}
public class CarList : List<Car> {
protected readonly Manager m_manager = null;
protected List<Action<object, TestCompletedEventArgs>> delegatesList = new List<Action<object, TestCompletedEventArgs>>();
public Manager Manager {
get { return m_manager; }
}
public CarList(Manager manager) {
m_manager = manager;
}
public void Test() {
foreach(Car car in this) {
bool ret = car.Test();
manager.Report(ret);
}
}
public void Add(TestCompletedEventHandler e) {
foreach (Car car in this) {
car.OnTestCompleted += e;
}
delegatesList.Add(e);
}
public void Add(Car car) {
foreach(Action a in delegatesList)
{
car.OnTestCompleted += a;
}
base.Add(car);
}
}
public class Car {
protected internal event TestCompletedEventHandler OnTestCompleted = null;
public bool Test() {
//...
if (OnTestCompleted != null) OnTestCompleted(this, new TestCompletedEventArgs());
}
}
public class TestCompletedEventArgs : EventArgs {
//...
}
}
using test;
Manager manager = new Manager();
Manager.Cars.Add(new Car());
manager.Cars.Add(new Car());
manager.Cars.Add(new Car());
manager.Cars.Add((sender, args) =>
{
//do whatever...
})
manager.Cars.Test();
manager.Cars.Add(new Car());