And i have one interface
public interface IMethod
{
String Add(string str);
Boolean Update(string str);
Boolean Delete(int id);
}
I have declared another Interface Like this which has IMethod as property.
public interface IFoo
{
IMethod MethodCaller { get ; set; }
}
Now I gone implement IFoo interface in my one of class and from which i want to call IMethods method.
Class implementation
public MyClass : IFoo
{
public IMethod MethodCaller{ get ; set; }
}
How do i do it ? how do i call Add Update Delete method from MyClass
MyClasses that implement IMethod as follows :
public class foo1:IMethod
{
public String Add(string str){ return string.Empty;}
Boolean Update(string str){//defination}
Boolean Delete(int id){ //defination}
}
public class foo2:IMethod
{
public String Add(string str){ return string.Empty;}
Boolean Update(string str){//defination}
Boolean Delete(int id){ //defination}
}
You still haven't defined any concrete class that implements IMethod
- you only have defined a property that is of type IMethod
- now you need to assign a concrete class to this property so you can call the methods on it. Once you have done that you can simply call methods on your MethodCaller
property:
string result = MethodCaller.Add(someFoo);
Inside a class:
public MyClass : IFoo
{
public void CallAllMethodsOfIIMethodImpl()
{
if (this.MethodCaller != null)
{
this.MethodCaller.Add( ... );
this.MethodCaller.Delete( ... );
this.MethodCaller.Update( ... );
}
}
}
Outside:
MyClass instance = new MyClass();
if (instance.MethodCaller != null)
{
instance.MethodCaller.Add( ... );
instance.MethodCaller.Delete( ... );
instance.MethodCaller.Update( ... );
}
Given myClass
is an instance of MyClass
and the MethodCaller
has been set to a concrete implementation, you can call the methods like this:
myClass.MethodCaller.Add(...);
myClass.MethodCaller.Update(...);
myClass.MethodCaller.Delete(...);
You have to create an inner class which implements
IMethod
interface.
public MyClass : IFoo
{
private TestClass _inst;
public IMethod MethodCaller
{
get
{
if(_inst==null)
_inst=new TestClass();
return _inst;
}
set
{
_inst=value;
}
}
public class TestClass : IMethod
{
public String Add(string str) {}
public Boolean Update(string str) {}
public Boolean Delete(int id) {}
}
}
Invoke methods:
MyClass instance=new MyClass();
instance.MethodCaller.Add(..);
OR
IMethod call=new MyClass().MethodCaller;
call.Add(..);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.