DeviceProperty结构:
public struct DeviceProperty
{
public string HardwareID;
}
IDevice接口:public interface IDevice
{
DeviceProperty Properties { get; }
}
Device类: internal class Device : IDevice
{
public DeviceProperty Properties
{
get { return properties; }
} }
DeviceInfo类:private IDevice[] Devices;
//委托:
delegate TResult Func<T, TResult>(T t); private IDevice[] FindDevice<T>(T t, Func<IDevice, bool> predicate)
{
if (null == Devices || 0 == Devices.Length)
return null; List<IDevice> array = new List<IDevice>();
foreach (IDevice element in Devices)
{
if (predicate(element))
array.Add(element);
}
return array.ToArray();
}
public IDevice[] FindByHardwareId(string value)
{
return FindDevice<string>(value, delegate(IDevice device) { return value == device.Properties.HardwareID; });
//return FindDevice<string>(value, TT);
}
//private TResult TT<T, TResult>(T device, TResult value)
//{
// return value == device.Properties.HardwareID;
//}委托就相当于C++ 中的函数指针,我就想弄清楚:Func<T, TResult>(T t) 调用的函数应该在 代码:
delegate(IDevice device) { return value == device.Properties.HardwareID;
如何把它单独给写出来
还有是有没有简单易懂的语句帮我理解泛型,委托的用法。
能否帮我分析DeviceInfo类:
中每个函数的意思?
希望大虾们指导一下了 谢谢了!
public struct DeviceProperty
{
public string HardwareID;
}
IDevice接口:public interface IDevice
{
DeviceProperty Properties { get; }
}
Device类: internal class Device : IDevice
{
public DeviceProperty Properties
{
get { return properties; }
} }
DeviceInfo类:private IDevice[] Devices;
//委托:
delegate TResult Func<T, TResult>(T t); private IDevice[] FindDevice<T>(T t, Func<IDevice, bool> predicate)
{
if (null == Devices || 0 == Devices.Length)
return null; List<IDevice> array = new List<IDevice>();
foreach (IDevice element in Devices)
{
if (predicate(element))
array.Add(element);
}
return array.ToArray();
}
public IDevice[] FindByHardwareId(string value)
{
return FindDevice<string>(value, delegate(IDevice device) { return value == device.Properties.HardwareID; });
//return FindDevice<string>(value, TT);
}
//private TResult TT<T, TResult>(T device, TResult value)
//{
// return value == device.Properties.HardwareID;
//}委托就相当于C++ 中的函数指针,我就想弄清楚:Func<T, TResult>(T t) 调用的函数应该在 代码:
delegate(IDevice device) { return value == device.Properties.HardwareID;
如何把它单独给写出来
还有是有没有简单易懂的语句帮我理解泛型,委托的用法。
能否帮我分析DeviceInfo类:
中每个函数的意思?
希望大虾们指导一下了 谢谢了!
这名是匿名函数的用法,等价于
private boolTT<IDevice, bool>(IDevice device, bool value)
{
return value == device.Properties.HardwareID;
}
private bool TT(IDevice device, bool value)
{
return value == device.Properties.HardwareID;
}
应该等价于:private bool TT(IDevice device)
//注意这里的委托只有一个参数而非2个参数:
//delegate TResult Func<T, TResult>(T t);
{
return “11” == device.Properties.HardwareID;
//匿名方法相当于少传入一个参数,此例中用"11"代替了 匿名方法中value的
//如果只用实名方法的话 ,应该把委托重新定义含有2个参数的,这样才能等价匿名方法的一个参数
}我又把我学的东西总结了一下的:
http://blog.csdn.net/xzjxylophone/archive/2009/11/24/4862148.aspx