Reputation: 81
What I am trying to do is actually really simple, but for some reason I am either going the wrong way about it or am just missing some vital know how, because I just cannot figure out how to call a method through an interface without creating a new instance of the class/interface.
Either way, what I am trying to achieve is to call the following method through an interface that implements the method, without having to create a new instance.
public partial class MyClass : IMyInterface
{
public void MyMethod(object obj)
{
//Method code....
}
}
my interface
public interface IMyInterface
{
void MyMethod(object obj);
}
My Solution has three Projects
the Interface and MyClass.xaml.cs
are in the UI, and I am trying to call the MyMethod()
method from a class in my Service layer by using an existing instance of the interface.
I have done it like this from another section in the code which does not work here, I have no idea why
//Register interface
internal static IMyInterface mi;
//and call it like so
mi.MyMethod(Object);
I have done plenty of google searches, but everyone seems to create a new instance which in my case is not a valid option.
Upvotes: 0
Views: 3408
Reputation: 2861
Going to Necro this.
I am going to offer an alternative approach/path that provides a different approach to implementing and execution of the Interface
.Net Fiddle (Included Content in the event the Short URL is purged)
public interface IMyInterface{
void MyMethod();
}
public class MyClass : IMyInterface{
// Implicit Implementation
public void MyMethod(){
Console.WriteLine("* Entered the Implicit Interface Method");
(this as IMyInterface).MyMethod();
}
// Explicit Implementation
void IMyInterface.MyMethod(){
Console.WriteLine("--> Entered the Explicit Interface Method");
}
}
using System;
public class Program
{
public static void Main()
{
var cl = new MyClass();
Console.WriteLine("Executing method off Object");
Console.WriteLine("--------------------------------");
cl.MyMethod();
Console.WriteLine("");
Console.WriteLine("");
Console.WriteLine("Executing method through Casting");
Console.WriteLine("--------------------------------");
(cl as IMyInterface).MyMethod();
}
}
Executing method off Object
--------------------------------
* Entered the Implicit Interface Method
--> Entered the Explicit Interface Method
Executing method through Casting
--------------------------------
--> Entered the Explicit Interface Method
Upvotes: 0
Reputation: 1291
by using an existing instance of the interface.
So, if you have created one instance of MyClass class, you could implement Singleton for it like this. So, your single instance would be available everywhere.
For example:
public partial class MyClass : IMyInterface
{
private static IMyInterface _instance;
public static IMyInterface Instance
{
get
{
if (_instance == null) _instance = new MyClass();
return _instance;
}
}
public void MyMethod(object obj)
{
//Method code....
}
}
And
//Register interface
internal static IMyInterface mi = MyClass.Instance;
//and call it like so
mi.MyMethod(Object);
Upvotes: 2
Reputation: 20754
You can not create an instance of interface. You can access an object via the interfaces it implements. For example if you have a List<T>
you can access it via IList<T>, ICollection<T>, IEnumerable<T>, IEnumerable, IList, ICollection, IReadOnlyList<T>,
IReadOnlyCollection<T>
.
Interfaces does not have any code or any implementation. You define an interface like IFruit
which has Color
and Taste
property and Eat
method but you do not implement anything in it. When you say Red Apple
class is implementing IFruit
then you implement the properties and method in that class.
In another word you can access objects through interfaces. You must have an object behind the interface.
Upvotes: 1
Reputation: 7903
what i am trying to achieve is to call the following method through an interface that implements the method, without having to create a new instance
interface that implements the method - is a wrong assumption. Interface never implements the method. It just says whoever implements this interface should implement that method.
The class who implements your interface - in your case MyClass
. You need to create an instance of it.
internal IMyInterface mi = new MyClass();
mi.MyMethod(Object);
There is no other way.
Upvotes: 3