本文整理汇总了C#中Type.IsValueType方法的典型用法代码示例。如果您正苦于以下问题:C# Type.IsValueType方法的具体用法?C# Type.IsValueType怎么用?C# Type.IsValueType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Type
的用法示例。
在下文中一共展示了Type.IsValueType方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetDefaultParameterValue
internal static object GetDefaultParameterValue(Type parameterType)
{
return (parameterType.IsValueType() && parameterType != typeof(void)) ? Activator.CreateInstance(parameterType) : null;
}
示例2: IsTypeNullable
private static bool IsTypeNullable(Type type)
{
return !type.IsValueType() ||
(type.IsGenericType() &&
type.GetGenericTypeDefinition() == typeof(Nullable<>));
}
示例3: IsSimpleType
// Return true if Json.Net will likely convert value of given type to a Json primitive, not JsonArray nor
// JsonObject.
// To do: https://aspnetwebstack.codeplex.com/workitem/1467
private static bool IsSimpleType(Type type)
{
Contract.Assert(type != null);
bool isSimpleType;
#if NETFX_CORE // TypeDescriptor is not supported in portable library
isSimpleType = type.IsValueType() || type == typeof(string);
#else
// CanConvertFrom() check is similar to MVC / Web API ModelMetadata.IsComplexType getters. This is
// sufficient for many cases but Json.Net uses JsonConverterAttribute and built-in converters, not type
// descriptors.
isSimpleType = TypeDescriptor.GetConverter(type).CanConvertFrom(typeof(string));
#endif
return isSimpleType;
}
示例4: IsCompatibleWith
static bool IsCompatibleWith(Type source, Type target)
{
#if !(NETFX_CORE || DNXCORE50)
if (source == target) return true;
if (!target.IsValueType) return target.IsAssignableFrom(source);
Type st = GetNonNullableType(source);
Type tt = GetNonNullableType(target);
if (st != source && tt == target) return false;
TypeCode sc = st.IsEnum() ? TypeCode.Object : Type.GetTypeCode(st);
TypeCode tc = tt.IsEnum() ? TypeCode.Object : Type.GetTypeCode(tt);
switch (sc)
{
case TypeCode.SByte:
switch (tc)
{
case TypeCode.SByte:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
case TypeCode.Single:
case TypeCode.Double:
case TypeCode.Decimal:
return true;
}
break;
case TypeCode.Byte:
switch (tc)
{
case TypeCode.Byte:
case TypeCode.Int16:
case TypeCode.UInt16:
case TypeCode.Int32:
case TypeCode.UInt32:
case TypeCode.Int64:
case TypeCode.UInt64:
case TypeCode.Single:
case TypeCode.Double:
case TypeCode.Decimal:
return true;
}
break;
case TypeCode.Int16:
switch (tc)
{
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
case TypeCode.Single:
case TypeCode.Double:
case TypeCode.Decimal:
return true;
}
break;
case TypeCode.UInt16:
switch (tc)
{
case TypeCode.UInt16:
case TypeCode.Int32:
case TypeCode.UInt32:
case TypeCode.Int64:
case TypeCode.UInt64:
case TypeCode.Single:
case TypeCode.Double:
case TypeCode.Decimal:
return true;
}
break;
case TypeCode.Int32:
switch (tc)
{
case TypeCode.Int32:
case TypeCode.Int64:
case TypeCode.Single:
case TypeCode.Double:
case TypeCode.Decimal:
return true;
}
break;
case TypeCode.UInt32:
switch (tc)
{
case TypeCode.UInt32:
case TypeCode.Int64:
case TypeCode.UInt64:
case TypeCode.Single:
case TypeCode.Double:
case TypeCode.Decimal:
return true;
}
break;
case TypeCode.Int64:
switch (tc)
{
case TypeCode.Int64:
case TypeCode.Single:
case TypeCode.Double:
case TypeCode.Decimal:
return true;
}
break;
//.........这里部分代码省略.........
示例5: CreateColumnMapFromReaderAndClrType
internal virtual CollectionColumnMap CreateColumnMapFromReaderAndClrType(
DbDataReader reader, Type type, MetadataWorkspace workspace)
{
DebugCheck.NotNull(reader);
DebugCheck.NotNull(type);
DebugCheck.NotNull(workspace);
// we require a default constructor
var constructor = type.GetDeclaredConstructor();
if (type.IsAbstract()
|| (null == constructor && !type.IsValueType()))
{
throw new InvalidOperationException(Strings.ObjectContext_InvalidTypeForStoreQuery(type));
}
// build a LINQ expression used by result assembly to create results
var memberInfo = new List<Tuple<MemberAssignment, int, EdmProperty>>();
foreach (var prop in type.GetInstanceProperties()
.Select(p => p.GetPropertyInfoForSet()))
{
// for enums unwrap the type if nullable
var propertyUnderlyingType = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType;
var propType = propertyUnderlyingType.IsEnum() ? propertyUnderlyingType.GetEnumUnderlyingType() : prop.PropertyType;
EdmType modelType;
int ordinal;
if (TryGetColumnOrdinalFromReader(reader, prop.Name, out ordinal)
&& workspace.TryDetermineCSpaceModelType(propType, out modelType)
&& (Helper.IsScalarType(modelType))
&& prop.CanWriteExtended()
&& prop.GetIndexParameters().Length == 0
&& null != prop.Setter())
{
memberInfo.Add(
Tuple.Create(
Expression.Bind(prop, Expression.Parameter(prop.PropertyType, "placeholder")),
ordinal,
new EdmProperty(prop.Name, TypeUsage.Create(modelType))));
}
}
// initialize members in the order in which they appear in the reader
var members = new MemberInfo[memberInfo.Count];
var memberBindings = new MemberBinding[memberInfo.Count];
var propertyMaps = new ColumnMap[memberInfo.Count];
var modelProperties = new EdmProperty[memberInfo.Count];
var i = 0;
foreach (var memberGroup in memberInfo.GroupBy(tuple => tuple.Item2).OrderBy(tuple => tuple.Key))
{
// make sure that a single column isn't contributing to multiple properties
if (memberGroup.Count() != 1)
{
throw new InvalidOperationException(
Strings.ObjectContext_TwoPropertiesMappedToSameColumn(
reader.GetName(memberGroup.Key),
String.Join(", ", memberGroup.Select(tuple => tuple.Item3.Name).ToArray())));
}
var member = memberGroup.Single();
var assignment = member.Item1;
var ordinal = member.Item2;
var modelProp = member.Item3;
members[i] = assignment.Member;
memberBindings[i] = assignment;
propertyMaps[i] = new ScalarColumnMap(modelProp.TypeUsage, modelProp.Name, 0, ordinal);
modelProperties[i] = modelProp;
i++;
}
var newExpr = null == constructor ? Expression.New(type) : Expression.New(constructor);
var init = Expression.MemberInit(newExpr, memberBindings);
var initMetadata = InitializerMetadata.CreateProjectionInitializer(
(EdmItemCollection)workspace.GetItemCollection(DataSpace.CSpace), init);
// column map (a collection of rows with InitializerMetadata markup)
var rowType = new RowType(modelProperties, initMetadata);
var rowMap = new RecordColumnMap(
TypeUsage.Create(rowType),
"DefaultTypeProjection", propertyMaps, null);
CollectionColumnMap collectionMap = new SimpleCollectionColumnMap(
rowType.GetCollectionType().TypeUsage,
rowType.Name, rowMap, null, null);
return collectionMap;
}
示例6: PromoteExpression
Expression PromoteExpression(Expression expr, Type type, bool exact)
{
if (expr.Type == type) return expr;
var ce = expr as ConstantExpression;
if (ce != null)
{
if (ce == _nullLiteral)
{
#if !(NETFX_CORE || DNXCORE50)
if (!type.IsValueType() || IsNullableType(type))
#else
if (!type.IsValueType() || IsNullableType(type))
#endif
return Expression.Constant(null, type);
}
else
{
string text;
if (_literals.TryGetValue(ce, out text))
{
Type target = GetNonNullableType(type);
Object value = null;
#if !(NETFX_CORE || DNXCORE50)
switch (Type.GetTypeCode(ce.Type))
{
case TypeCode.Int32:
case TypeCode.UInt32:
case TypeCode.Int64:
case TypeCode.UInt64:
value = ParseNumber(text, target);
break;
case TypeCode.Double:
if (target == typeof(decimal)) value = ParseNumber(text, target);
break;
case TypeCode.String:
value = ParseEnum(text, target);
break;
}
#else
if (ce.Type == typeof(Int32) || ce.Type == typeof(UInt32) || ce.Type == typeof(Int64) || ce.Type == typeof(UInt64))
value = ParseNumber(text, target);
else if (ce.Type == typeof(Double))
{
if (target == typeof(decimal)) value = ParseNumber(text, target);
}
else if (ce.Type == typeof(String))
value = ParseEnum(text, target);
#endif
if (value != null)
return Expression.Constant(value, type);
}
}
}
if (IsCompatibleWith(expr.Type, type))
{
#if !(NETFX_CORE || DNXCORE50)
if (type.IsValueType || exact) return Expression.Convert(expr, type);
#else
if (type.IsValueType() || exact) return Expression.Convert(expr, type);
#endif
return expr;
}
return null;
}
示例7: GenerateConversion
static Expression GenerateConversion(Expression expr, Type type, int errorPos)
{
Type exprType = expr.Type;
if (exprType == type)
return expr;
if (exprType.IsValueType() && type.IsValueType())
{
if ((IsNullableType(exprType) || IsNullableType(type)) &&
GetNonNullableType(exprType) == GetNonNullableType(type))
return Expression.Convert(expr, type);
if ((IsNumericType(exprType) || IsEnumType(exprType)) &&
(IsNumericType(type)) || IsEnumType(type))
return Expression.ConvertChecked(expr, type);
}
if (exprType.IsAssignableFrom(type) || type.IsAssignableFrom(exprType) || exprType.IsInterface() || type.IsInterface())
return Expression.Convert(expr, type);
// Try to Parse the string rather that just generate the convert statement
if (expr.NodeType == ExpressionType.Constant && exprType == typeof(string))
{
string text = (string)((ConstantExpression)expr).Value;
// DateTime is parsed as UTC time.
DateTime dateTime;
if (type == typeof(DateTime) && DateTime.TryParse(text, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime))
return Expression.Constant(dateTime, type);
object[] arguments = { text, null };
#if DNXCORE50
MethodInfo method = type.GetMethod("TryParse", new [] { typeof(string), type.MakeByRefType() });
#else
MethodInfo method = type.GetMethod("TryParse", BindingFlags.Public | BindingFlags.Static, null, new Type[] { typeof(string), type.MakeByRefType() }, null);
#endif
if (method != null && (bool)method.Invoke(null, arguments))
return Expression.Constant(arguments[1], type);
}
throw ParseError(errorPos, Res.CannotConvertValue, GetTypeName(exprType), GetTypeName(type));
}
示例8: ParseTypeAccess
Expression ParseTypeAccess(Type type)
{
int errorPos = _token.pos;
NextToken();
if (_token.id == TokenId.Question)
{
if (!type.IsValueType() || IsNullableType(type))
throw ParseError(errorPos, Res.TypeHasNoNullableForm, GetTypeName(type));
type = typeof(Nullable<>).MakeGenericType(type);
NextToken();
}
// This is a shorthand for explicitely converting a string to something
//
bool shorthand = _token.id == TokenId.StringLiteral;
if (_token.id == TokenId.OpenParen || shorthand)
{
Expression[] args = shorthand
? new Expression[] { ParseStringLiteral() }
: ParseArgumentList();
MethodBase method;
switch (FindBestMethod(type.GetConstructors(), args, out method))
{
case 0:
if (args.Length == 1)
return GenerateConversion(args[0], type, errorPos);
throw ParseError(errorPos, Res.NoMatchingConstructor, GetTypeName(type));
case 1:
return Expression.New((ConstructorInfo)method, args);
default:
throw ParseError(errorPos, Res.AmbiguousConstructorInvocation, GetTypeName(type));
}
}
ValidateToken(TokenId.Dot, Res.DotOrOpenParenOrStringLiteralExpected);
NextToken();
return ParseMemberAccess(type, null);
}
示例9: GenerateConversion
static Expression GenerateConversion(Expression expr, Type type, int errorPos)
{
Type exprType = expr.Type;
if (exprType == type)
return expr;
if (exprType.IsValueType() && type.IsValueType())
{
if ((IsNullableType(exprType) || IsNullableType(type)) &&
GetNonNullableType(exprType) == GetNonNullableType(type))
return Expression.Convert(expr, type);
if ((IsNumericType(exprType) || IsEnumType(exprType)) &&
(IsNumericType(type)) || IsEnumType(type))
return Expression.ConvertChecked(expr, type);
}
if (exprType.IsAssignableFrom(type) || type.IsAssignableFrom(exprType) || exprType.IsInterface() || type.IsInterface())
return Expression.Convert(expr, type);
throw ParseError(errorPos, Res.CannotConvertValue, GetTypeName(exprType), GetTypeName(type));
}
示例10: ParseTypeAccess
Expression ParseTypeAccess(Type type)
{
int errorPos = _token.pos;
NextToken();
if (_token.id == TokenId.Question)
{
if (!type.IsValueType() || IsNullableType(type))
throw ParseError(errorPos, Res.TypeHasNoNullableForm, GetTypeName(type));
type = typeof(Nullable<>).MakeGenericType(type);
NextToken();
}
if (_token.id == TokenId.OpenParen)
{
Expression[] args = ParseArgumentList();
MethodBase method;
#if !(NETFX_CORE || DNXCORE50)
switch (FindBestMethod(type.GetConstructors(), args, out method))
#else
switch (FindBestMethod(type.GetTypeInfo().DeclaredConstructors, args, out method))
#endif
{
case 0:
if (args.Length == 1)
return GenerateConversion(args[0], type, errorPos);
throw ParseError(errorPos, Res.NoMatchingConstructor, GetTypeName(type));
case 1:
return Expression.New((ConstructorInfo)method, args);
default:
throw ParseError(errorPos, Res.AmbiguousConstructorInvocation, GetTypeName(type));
}
}
ValidateToken(TokenId.Dot, Res.DotOrOpenParenExpected);
NextToken();
return ParseMemberAccess(type, null);
}