从Codeplex下了一个代码,其中出异常的代码,出现这样的操作符。本人比较菜,希望高手指点下。代码如下, public static T Clone<T>(T source, Dictionary<Type, InstantiationHandler> typeInstantiationHandlers, List<PropertyInfo> ignoredProperties)
{
if (source == null)
throw new ArgumentNullException("source"); ignoredProperties = ignoredProperties ?? new List<PropertyInfo>();
typeInstantiationHandlers = typeInstantiationHandlers ?? new Dictionary<Type, InstantiationHandler>();
Type sourceType = source.GetType(); T cloned;
InstantiationHandler handler;
if (typeInstantiationHandlers.TryGetValue(sourceType, out handler))
cloned = (T)handler(source);
else
cloned = (T)Activator.CreateInstance(sourceType); foreach (PropertyInfo curPropInfo in sourceType.GetProperties())
{
if (curPropInfo.GetGetMethod() != null
&& (curPropInfo.GetSetMethod() != null))
{
if (!ignoredProperties.Contains(curPropInfo))
{
// Handle Non-indexer properties
if (curPropInfo.Name != "Item")
{
// get property from source
object getValue = curPropInfo.GetGetMethod().Invoke(source, new object[] { });
object clonedValue = curPropInfo.GetGetMethod().Invoke(cloned, new object[] { }); if (getValue != clonedValue)
{
// clone if needed
if (getValue != null && getValue is DependencyObject)
getValue = Clone((DependencyObject)getValue, typeInstantiationHandlers); // set property on cloned
curPropInfo.GetSetMethod().Invoke(cloned, new object[] { getValue });
}
}
// handle indexer
else
{
// get count for indexer
int numberofItemInColleciton =
(int)
curPropInfo.ReflectedType.GetProperty("Count").GetGetMethod().Invoke(source, new object[] { }); // run on indexer
for (int i = 0; i < numberofItemInColleciton; i++)
{
// get item through Indexer
object getValue = curPropInfo.GetGetMethod().Invoke(source, new object[] { i }); // clone if needed
if (getValue != null && getValue is DependencyObject)
getValue = Clone((DependencyObject)getValue, typeInstantiationHandlers);
// add item to collection
curPropInfo.ReflectedType.GetMethod("Add").Invoke(cloned, new object[] { getValue });
}
}
}
}
} return cloned;
}
先谢谢了
{
if (source == null)
throw new ArgumentNullException("source"); ignoredProperties = ignoredProperties ?? new List<PropertyInfo>();
typeInstantiationHandlers = typeInstantiationHandlers ?? new Dictionary<Type, InstantiationHandler>();
Type sourceType = source.GetType(); T cloned;
InstantiationHandler handler;
if (typeInstantiationHandlers.TryGetValue(sourceType, out handler))
cloned = (T)handler(source);
else
cloned = (T)Activator.CreateInstance(sourceType); foreach (PropertyInfo curPropInfo in sourceType.GetProperties())
{
if (curPropInfo.GetGetMethod() != null
&& (curPropInfo.GetSetMethod() != null))
{
if (!ignoredProperties.Contains(curPropInfo))
{
// Handle Non-indexer properties
if (curPropInfo.Name != "Item")
{
// get property from source
object getValue = curPropInfo.GetGetMethod().Invoke(source, new object[] { });
object clonedValue = curPropInfo.GetGetMethod().Invoke(cloned, new object[] { }); if (getValue != clonedValue)
{
// clone if needed
if (getValue != null && getValue is DependencyObject)
getValue = Clone((DependencyObject)getValue, typeInstantiationHandlers); // set property on cloned
curPropInfo.GetSetMethod().Invoke(cloned, new object[] { getValue });
}
}
// handle indexer
else
{
// get count for indexer
int numberofItemInColleciton =
(int)
curPropInfo.ReflectedType.GetProperty("Count").GetGetMethod().Invoke(source, new object[] { }); // run on indexer
for (int i = 0; i < numberofItemInColleciton; i++)
{
// get item through Indexer
object getValue = curPropInfo.GetGetMethod().Invoke(source, new object[] { i }); // clone if needed
if (getValue != null && getValue is DependencyObject)
getValue = Clone((DependencyObject)getValue, typeInstantiationHandlers);
// add item to collection
curPropInfo.ReflectedType.GetMethod("Add").Invoke(cloned, new object[] { getValue });
}
}
}
}
} return cloned;
}
先谢谢了
a=null;//可空类型可以为null
int b=a??0;//判断a是否为null,如果为null则b=0,如果不为null则b等于a.value;
判断??左边表达式是否为null,如是null则取右边表达式的值,否则就取左边表达式的值
int? a; a = null;
a = 10; int b = a ?? 0; //返回b=10