本文整理汇总了C#中System.Type.IsValueType方法的典型用法代码示例。如果您正苦于以下问题:C# Type.IsValueType方法的具体用法?C# Type.IsValueType怎么用?C# Type.IsValueType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Type
的用法示例。
在下文中一共展示了Type.IsValueType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetConversionType
public static BasicTypes GetConversionType(Type innType, Type outType)
{
if (innType.Implements<IDictionary>() && outType.Implements<IDictionary>())
return BasicTypes.Dictionary;
if (innType.Implements<IEnumerable>() && outType.Implements<IEnumerable>() && !innType.IsValueType())
return BasicTypes.List;
if (innType.IsValueType() && outType.IsValueType() || TypeExtensions.CanConvert(outType, innType))
return BasicTypes.Convertable;
return BasicTypes.ComplexType;
}
示例2: TryCast
public static object TryCast(object value, Type targetType) {
Type underlyingType = Nullable.GetUnderlyingType(targetType) ?? targetType;
#if !NETFX_CORE
if(underlyingType.IsEnum && value is string) {
#else
if(underlyingType.IsEnum() && value is string) {
#endif
value = Enum.Parse(underlyingType, (string)value, false);
} else if(
#if !NETFX_CORE
value is IConvertible &&
#else
IsConvertableType(value) &&
#endif
!targetType.IsAssignableFrom(value.GetType())) {
value = Convert.ChangeType(value, underlyingType, CultureInfo.InvariantCulture);
}
#if !NETFX_CORE
if(value == null && targetType.IsValueType)
#else
if(value == null && targetType.IsValueType())
#endif
value = Activator.CreateInstance(targetType);
return value;
}
示例3: DeserializeFromString
public static object DeserializeFromString(string reader, Type type, Options options)
{
var cached = (Func<string, Options, object>)DeserializeFromStringIndirectCache[type];
if (cached == null)
{
lock (DeserializeFromStreamIndirectCache)
{
cached = (Func<string, Options, object>)DeserializeFromStringIndirectCache[type];
if (cached == null)
{
var emit = Emit.NewDynamicMethod(typeof(object), new[] { typeof(string), typeof(Options) }, doVerify: Utils.DoVerify);
var mtd = JSONDeserializeFromString.MakeGenericMethod(type);
emit.LoadArgument(0); // TextReader
emit.LoadArgument(1); // TextReader Options
emit.Call(mtd); // type
if (type.IsValueType())
{
emit.Box(type); // object
}
emit.Return();
DeserializeFromStringIndirectCache[type] = cached = emit.CreateDelegate<Func<string, Options, object>>(Utils.DelegateOptimizationOptions);
}
}
}
return cached(reader, options);
}
示例4: Create
public static Instruction Create(Type type)
{
// Boxed enums can be unboxed as their underlying types:
switch ((type.IsEnum() ? Enum.GetUnderlyingType(type) : type).GetTypeCode()) {
case TypeCode.Boolean: return _Boolean ?? (_Boolean = new NotEqualBoolean());
case TypeCode.SByte: return _SByte ?? (_SByte = new NotEqualSByte());
case TypeCode.Byte: return _Byte ?? (_Byte = new NotEqualByte());
case TypeCode.Char: return _Char ?? (_Char = new NotEqualChar());
case TypeCode.Int16: return _Int16 ?? (_Int16 = new NotEqualInt16());
case TypeCode.Int32: return _Int32 ?? (_Int32 = new NotEqualInt32());
case TypeCode.Int64: return _Int64 ?? (_Int64 = new NotEqualInt64());
case TypeCode.UInt16: return _UInt16 ?? (_UInt16 = new NotEqualInt16());
case TypeCode.UInt32: return _UInt32 ?? (_UInt32 = new NotEqualInt32());
case TypeCode.UInt64: return _UInt64 ?? (_UInt64 = new NotEqualInt64());
case TypeCode.Single: return _Single ?? (_Single = new NotEqualSingle());
case TypeCode.Double: return _Double ?? (_Double = new NotEqualDouble());
case TypeCode.Object:
if (!type.IsValueType()) {
return _Reference ?? (_Reference = new NotEqualReference());
}
// TODO: Nullable<T>
throw new NotImplementedException();
default:
throw new NotImplementedException();
}
}
示例5: UnboxIfNeeded
public static void UnboxIfNeeded(this ILGenerator generator, Type type)
{
if (type.IsValueType())
generator.Emit(OpCodes.Unbox_Any, type);
else
generator.Emit(OpCodes.Castclass, type);
}
示例6: CreateNonNullValue
public static object CreateNonNullValue(Type type)
{
return type.IsValueType()
? CreateObject(type)
: type == typeof (string)
? string.Empty
: CreateObject(type);
}
示例7: PushInstance
public static void PushInstance(this ILGenerator generator, Type type)
{
generator.Emit(OpCodes.Ldarg_0);
if (type.IsValueType())
generator.Emit(OpCodes.Unbox, type);
else
generator.Emit(OpCodes.Castclass, type);
}
示例8: TypeAllowsNull
/// <summary>Checks whether the specified <paramref name='type' /> can be assigned null.</summary>
/// <param name='type'>Type to check.</param>
/// <returns>true if type is a reference type or a Nullable type; false otherwise.</returns>
internal static bool TypeAllowsNull(Type type)
{
Debug.Assert(type != null, "type != null");
//// This is a copy of WebUtil.TypeAllowsNull from the product.
return !type.IsValueType() || IsNullableType(type);
}
示例9: TypeAllowsNull
internal static bool TypeAllowsNull(Type type)
{
if (type.IsValueType())
{
return IsNullableType(type);
}
return true;
}
示例10: BoxIfNeeded
public static void BoxIfNeeded(this ILGenerator generator, Type type)
{
if (type.IsValueType())
{
generator.Emit(OpCodes.Box, type);
}
else
{
generator.Emit(OpCodes.Castclass, type);
}
}
示例11: ConvertExpression
static LambdaExpression ConvertExpression(Type sourceType, Type destinationType)
{
bool nullableDestination;
var underlyingDestinationType = UnderlyingType(destinationType, out nullableDestination);
var convertMethod = typeof(Convert).GetDeclaredMethod("To" + underlyingDestinationType.Name, new[] { sourceType });
var sourceParameter = Parameter(sourceType, "source");
Expression convertCall = Call(convertMethod, sourceParameter);
var lambdaBody = nullableDestination && !sourceType.IsValueType() ?
Condition(Equal(sourceParameter, Constant(null)), Constant(null, destinationType), ToType(convertCall, destinationType)) :
convertCall;
return Lambda(lambdaBody, sourceParameter);
}
示例12: EmitTypeConversion
static void EmitTypeConversion(ILGenerator generator, Type castType, bool isContainer)
{
if (castType == typeof(object))
{
}
else if (castType.IsValueType())
{
generator.Emit(isContainer ? OpCodes.Unbox : OpCodes.Unbox_Any, castType);
}
else
{
generator.Emit(OpCodes.Castclass, castType);
}
}
示例13: Convert
/// <summary>
/// Converts an object at runtime into the specified type.
/// </summary>
public virtual object Convert(object obj, Type toType) {
if (obj == null) {
if (!toType.IsValueType()) {
return null;
}
} else {
if (toType.IsValueType()) {
if (toType == obj.GetType()) {
return obj;
}
} else {
if (toType.IsAssignableFrom(obj.GetType())) {
return obj;
}
}
}
throw Error.InvalidCast(obj != null ? obj.GetType().Name : "(null)", toType.Name);
}
示例14: GetValue
public object GetValue(Type targetType)
{
var stringValue = _underlyingValue as string;
if (_underlyingValue == null)
{
if (targetType.IsValueType() && !(targetType.IsGenericType() && targetType.GetGenericTypeDefinition() == typeof(Nullable<>)))
{
var valueAsString = string.IsNullOrEmpty(stringValue) ? "<null>" : string.Format("\"{0}\"", _underlyingValue);
throw new ArgumentException(string.Format("Cannot convert {0} to {1} (Column: '{2}', Row: {3})", valueAsString, targetType.Name, Header, Row));
}
ValueHasBeenUsed = true;
return null;
}
ValueHasBeenUsed = true;
if (targetType.IsInstanceOfType(_underlyingValue))
return _underlyingValue;
if (targetType.IsEnum() && _underlyingValue is string)
return Enum.Parse(targetType, (string)_underlyingValue);
if (targetType == typeof(DateTime))
return DateTime.Parse(stringValue);
try
{
return Convert.ChangeType(_underlyingValue, targetType);
}
catch (InvalidCastException ex)
{
throw new UnassignableExampleException(string.Format(
"{0} cannot be assigned to {1} (Column: '{2}', Row: {3})",
_underlyingValue == null ? "<null>" : _underlyingValue.ToString(),
targetType.Name, Header, Row), ex, this);
}
}
示例15: EmitLoadIndirectOpCodeForType
/// <summary>
/// Emits a load indirect opcode of the appropriate type for a value or object reference.
/// Pops a pointer off the evaluation stack, dereferences it and loads
/// a value of the specified type.
/// </summary>
/// <param name = "gen"></param>
/// <param name = "type"></param>
public static void EmitLoadIndirectOpCodeForType(ILGenerator gen, Type type)
{
if (type.IsEnum())
{
EmitLoadIndirectOpCodeForType(gen, GetUnderlyingTypeOfEnum(type));
return;
}
if (type.IsByRef)
{
throw new NotSupportedException("Cannot load ByRef values");
}
else if (type.IsPrimitive() && type != typeof(IntPtr))
{
var opCode = LdindOpCodesDictionary.Instance[type];
if (opCode == LdindOpCodesDictionary.EmptyOpCode)
{
throw new ArgumentException("Type " + type + " could not be converted to a OpCode");
}
gen.Emit(opCode);
}
else if (type.IsValueType())
{
gen.Emit(OpCodes.Ldobj, type);
}
else if (type.IsGenericParameter)
{
gen.Emit(OpCodes.Ldobj, type);
}
else
{
gen.Emit(OpCodes.Ldind_Ref);
}
}