2010-06-08 79 views
2

通用接口所以,首先我有我的命令属性界面与方法指针

public interface ICommandProperty<T, U> 
{ 
    Func<T> CreateCommand { get; set; } 
    Func<T, U> ParseResponse { get; set; } 
} 

的想法是,我可以创建一个简单的解析器,需要一个字符串,并返回一个ip地址例如。

这个接口,然后在另一个接口用于:

public interface IDeviceCommand 
{ 

    Func<ICommandProperty<object, object>> SetCommand 
    { 
     get; 
     set; 
    } 
    Func<ICommandProperty<object, object>> GetCommand 
    { 
     get; 
     set; 
    } 
    string Name { get; set; } 
} 

我可能会对此都错了,但是这是我的问题。目前我有泛型接口声明与对象,因为我不知道一种方法来设置它们的一般(IDeviceCommand不能是通用的各种原因)。

我的具体实施看起来是这样的:

public class DeviceCommand:IDeviceCommand 
{ 
    public DeviceCommand(string name,Func<ICommandProperty<object,object>> Set,Func<ICommandProperty<object,object>> Get) 
    { 
     this.Name = name; 
     this.SetCommand = Set; 
     this.GetCommand = Get; 
    } 
    #region IDeviceCommand Members 
    public string Name 
    { 
     get; 
     set; 
    } 
    public object Value 
    { 
     get; 
     set; 
    } 
    public Func<ICommandProperty<object, object>> SetCommand 
    { 
     get; 
     set; 
    } 
    public Func<ICommandProperty<object, object>> GetCommand 
    { 
     get; 
     set; 
    } 
    #endregion 
} 

我可以做DeviceCommand是一个通用类,并用T,U的set命令和GetCommand,但后来它不满足IDeviceCommand接口,因为Func<ICommandProperty<T,U>> isn't Func<ICommandProperty<object,object>>

是否有不同的方法,我应该在这里使用。实质上,我试图创建一个方法指针,当我实例化DeviceCommand时,可以设置它。

回答

1

你的问题似乎有点含糊,但这里有一些想法。首先是考虑使用方法而不是属性,以便它们可以是泛型的而不是类。不知道这是否适合你。您也可以考虑将“构建器”对象传递到您的设备命令中,而不是“func <”的本身。最后,这会让客户知道要问什么,并确保它与具有正确功能的对象一起工作。在这种情况下,也许像IDeviceCommand.Create和.Parse方法可以为你工作。

最后,如果您一直在寻找某个字符串并返回IP,那么泛型可能不是必需的。即使是普通的老代表也可以探索。

public interface ICommandProperty<T, U> 
    { 
     Func<T> CreateCommand { get; set; } 
     Func<T, U> ParseResponse { get; set; } 
    } 

    public interface IDeviceCommand 
    { 

     void SetCreateCommand<T, U>(Func<ICommandProperty<T, U>> cmd); 
     void SetParseCommand<T, U>(Func<ICommandProperty<T, U>> cmd); 

     Func<ICommandProperty<T, U>> GetCreateCommand<T, U>(); 
     Func<ICommandProperty<T, U>> GetParseCommand<T, U>(); 

     void Create(object someKnownObject); 
     T Parse<T>(object someKnownObject); 

     string Name { get; set; } 
    } 

    public class DeviceCommand : IDeviceCommand 
    { 
     public DeviceCommand(IDeviceCommandBuilder builder) 
     { 
      builder.SetCommands(this); 
     } 


     public void SetCreateCommand<T, U>(Func<ICommandProperty<T, U>> cmd) 
     { 
      throw new NotImplementedException(); 
     } 

     public void SetParseCommand<T, U>(Func<ICommandProperty<T, U>> cmd) 
     { 
      throw new NotImplementedException(); 
     } 

     public Func<ICommandProperty<T, U>> GetCreateCommand<T, U>() 
     { 
      throw new NotImplementedException(); 
     } 

     public Func<ICommandProperty<T, U>> GetParseCommand<T, U>() 
     { 
      throw new NotImplementedException(); 
     } 

     public void Create(object someKnownObject) 
     { 
      throw new NotImplementedException(); 
     } 

     public T Parse<T>(object someKnownObject) 
     { 
      throw new NotImplementedException(); 
     } 

     public string Name 
     { 
      get { throw new NotImplementedException(); } 
      set { throw new NotImplementedException(); } 
     } 
    } 

    public interface IDeviceCommandBuilder 
    { 
     void SetCommands(IDeviceCommand command); 
    } 

    public class DeviceCommandBuilder : IDeviceCommandBuilder 
    { 
     public void SetCommands(IDeviceCommand command) 
     { 
      command.SetCreateCommand<string,Uri>(.) 
      ; 
      command.SetParseCommand(.); 
     } 
    } 
+0

科里,谢谢你的回应,你得到了我后肯定的jist。你说得对,主要问题是客户不一定知道需要的类型。所以,当我使用IDeviceCommand.GetCreateCommand ()时,调用者不会知道T,U传递哪些类型。 – BigTundra 2010-06-10 17:18:23