对象“Intelligence”,对象“Intelligences”,这两个对象的属性、方法、字段等都完全一样,只有名字不一样。而且这两个对象对应的类不在同一个命名空间下。问题一: “Intelligence”的对象集合“List<Intelligence> testone”,能不能直接转化为“List<Intelligences>”的对象集合???
List<Intelligence> testone = new List<Intelligence>(); List<Intelligences> testtwo=List<Intelligences>(testone); 这样行吗??? 为什么???问题二: 自定义“KeyValuePairInfo”类如下。 [Serializable]
public class KeyValuePairInfo<TKey, TValue>
{
private TKey _key;
private TValue _value; public KeyValuePairInfo(TKey key, TValue value)
{
_key = key;
_value = value;
} public TKey Key
{
get { return _key; }
set { _key = value; }
} public TValue Value
{
get { return _value; }
set { _value = value; }
}
} 自定义的“KeyValuePairInfo”类型声明的参数,可不可以和.Net内置的“KeyValuePair”类型声明的参数进行参数类型转化??? KeyValuePairInfo<Monitorings.Period, string> dateone; KeyValuePair <Monitorings.Period, string> datetwo = KeyValuePair <Monitorings.Period, string>( dateone); 这样可以吗???这样可以吗???有什么地方不对的吗???
List<Intelligence> testone = new List<Intelligence>(); List<Intelligences> testtwo=List<Intelligences>(testone); 这样行吗??? 为什么???问题二: 自定义“KeyValuePairInfo”类如下。 [Serializable]
public class KeyValuePairInfo<TKey, TValue>
{
private TKey _key;
private TValue _value; public KeyValuePairInfo(TKey key, TValue value)
{
_key = key;
_value = value;
} public TKey Key
{
get { return _key; }
set { _key = value; }
} public TValue Value
{
get { return _value; }
set { _value = value; }
}
} 自定义的“KeyValuePairInfo”类型声明的参数,可不可以和.Net内置的“KeyValuePair”类型声明的参数进行参数类型转化??? KeyValuePairInfo<Monitorings.Period, string> dateone; KeyValuePair <Monitorings.Period, string> datetwo = KeyValuePair <Monitorings.Period, string>( dateone); 这样可以吗???这样可以吗???有什么地方不对的吗???
public static implicit operator KeyValuePairInfo(KeyValuePair value) { return new KeyValuePairInfo(value.Key, value.Value); }
B.User t = m;
此时m和t是两个不同的对象,对于使用者来说会很疑惑,很迷茫。引用类型如果没有继承关系最好不要做转换(包括隐式和显式)