[英]Generic Interface with method pointer
所以,首先我有我的命令屬性界面
public interface ICommandProperty<T, U>
{
Func<T> CreateCommand { get; set; }
Func<T, U> ParseResponse { get; set; }
}
我的想法是,我可以創建一個簡單的解析器,該解析器接受一個字符串並返回一個IPAddress例如。
然后在另一個接口中使用此接口:
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成為通用類,並在SetCommand和GetCommand上使用T,U,但是它不滿足IDeviceCommand接口,因為Func<ICommandProperty<T,U>> isn't Func<ICommandProperty<object,object>>
我在這里應該使用其他方法嗎? 本質上,我試圖創建實例化DeviceCommand時可以設置的方法指針。
您的問題似乎有點含糊,但是這里有一些想法。 首先是考慮使用方法而不是屬性,以便它們可以是通用的而不是類。 不確定是否適合您。 您也可以考慮將“ builder”對象而不是func <>本身傳遞給Device命令。 最后,這會讓您的客戶不知所措,知道要問什么,並確保它正在與具有正確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(.);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.