Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何调用类中实现的接口的方法?_C#_Interface - Fatal编程技术网

C# 如何调用类中实现的接口的方法?

C# 如何调用类中实现的接口的方法?,c#,interface,C#,Interface,我有一个接口 public interface IMethod { String Add(string str); Boolean Update(string str); Boolean Delete(int id); } 我已经声明了另一个类似这样的接口 这是我的财产 public interface IFoo { IMethod MethodCaller { get ; set; } } 现在我在我的of类中实现了IFoo接口,我想从

我有一个接口

 public  interface IMethod
 {
     String Add(string str);
     Boolean Update(string str);
     Boolean Delete(int id);
 }
我已经声明了另一个类似这样的接口 这是我的财产

public interface IFoo
{
     IMethod MethodCaller { get  ; set; }  
}
现在我在我的of类中实现了IFoo接口,我想从中调用IMethods方法

类实现

 public MyClass : IFoo
 { 
     public IMethod MethodCaller{ get  ; set; }  
 }
我该怎么做?如何从MyClass

MyClass实现了如下方法:

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}
}
public MyClass : IFoo   
{
   public void CallAllMethodsOfIIMethodImpl()
   {
       if (this.MethodCaller != null)
       {
          this.MethodCaller.Add( ... );
          this.MethodCaller.Delete( ... );
          this.MethodCaller.Update( ... );
       }
   }
}
MyClass instance = new MyClass();
if (instance.MethodCaller != null)
{
   instance.MethodCaller.Add( ... );
   instance.MethodCaller.Delete( ... );
   instance.MethodCaller.Update( ... );
}

您仍然没有定义任何实现
IMethod
的具体类-您只定义了一个类型为
IMethod
的属性-现在您需要为该属性指定一个具体类,以便可以对其调用方法。完成此操作后,只需调用
MethodCaller
属性上的方法即可:

string result = MethodCaller.Add(someFoo);

如果
myClass
myClass
的一个实例,并且
MethodCaller
已被设置为一个具体的实现,则可以调用如下方法:

myClass.MethodCaller.Add(...);
myClass.MethodCaller.Update(...);
myClass.MethodCaller.Delete(...);

类内:

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}
}
public MyClass : IFoo   
{
   public void CallAllMethodsOfIIMethodImpl()
   {
       if (this.MethodCaller != null)
       {
          this.MethodCaller.Add( ... );
          this.MethodCaller.Delete( ... );
          this.MethodCaller.Update( ... );
       }
   }
}
MyClass instance = new MyClass();
if (instance.MethodCaller != null)
{
   instance.MethodCaller.Add( ... );
   instance.MethodCaller.Delete( ... );
   instance.MethodCaller.Update( ... );
}
外部:

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}
}
public MyClass : IFoo   
{
   public void CallAllMethodsOfIIMethodImpl()
   {
       if (this.MethodCaller != null)
       {
          this.MethodCaller.Add( ... );
          this.MethodCaller.Delete( ... );
          this.MethodCaller.Update( ... );
       }
   }
}
MyClass instance = new MyClass();
if (instance.MethodCaller != null)
{
   instance.MethodCaller.Add( ... );
   instance.MethodCaller.Delete( ... );
   instance.MethodCaller.Update( ... );
}

您必须创建一个内部类,
实现
IMethod
接口

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) {}
   }
 }
调用方法:

MyClass instance=new MyClass();
instance.MethodCaller.Add(..);


? 实现IMethod的类是什么?MyClass只实现IFoo。