本文整理汇总了C#中System.Type.GetArrayRank方法的典型用法代码示例。如果您正苦于以下问题:C# Type.GetArrayRank方法的具体用法?C# Type.GetArrayRank怎么用?C# Type.GetArrayRank使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Type
的用法示例。
在下文中一共展示了Type.GetArrayRank方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ArrayDescriptor
public ArrayDescriptor(ITypeDescriptorFactory factory, Type type, bool emitDefaultValues, IMemberNamingConvention namingConvention)
: base(factory, type, emitDefaultValues, namingConvention)
{
if (!type.IsArray) throw new ArgumentException(@"Expecting array type", nameof(type));
if (type.GetArrayRank() != 1)
{
throw new ArgumentException("Cannot support dimension [{0}] for type [{1}]. Only supporting dimension of 1".ToFormat(type.GetArrayRank(), type.FullName));
}
ElementType = type.GetElementType();
}
示例2: ArrayDescriptor
/// <summary>
/// Initializes a new instance of the <see cref="ObjectDescriptor" /> class.
/// </summary>
/// <param name="attributeRegistry">The attribute registry.</param>
/// <param name="type">The type.</param>
/// <exception cref="System.ArgumentException">Expecting arrat type;type</exception>
public ArrayDescriptor(IAttributeRegistry attributeRegistry, Type type)
: base(attributeRegistry, type, false)
{
if (!type.IsArray) throw new ArgumentException("Expecting array type", "type");
if (type.GetArrayRank() != 1)
{
throw new ArgumentException("Cannot support dimension [{0}] for type [{1}]. Only supporting dimension of 1".DoFormat(type.GetArrayRank(), type.FullName));
}
elementType = type.GetElementType();
listType = typeof(List<>).MakeGenericType(ElementType);
toArrayMethod = listType.GetMethod("ToArray");
}
示例3: ArrayDescriptor
/// <summary>
/// Initializes a new instance of the <see cref="ObjectDescriptor" /> class.
/// </summary>
/// <param name="factory">The factory.</param>
/// <param name="type">The type.</param>
/// <exception cref="System.ArgumentException">Expecting arrat type;type</exception>
public ArrayDescriptor(ITypeDescriptorFactory factory, Type type)
: base(factory, type)
{
if (!type.IsArray) throw new ArgumentException("Expecting array type", "type");
if (type.GetArrayRank() != 1)
{
throw new ArgumentException("Cannot support dimension [{0}] for type [{1}]. Only supporting dimension of 1".ToFormat(type.GetArrayRank(), type.FullName));
}
Category = DescriptorCategory.Array;
elementType = type.GetElementType();
listType = typeof(List<>).MakeGenericType(ElementType);
toArrayMethod = listType.GetMethod("ToArray");
}
示例4: ArrayTypeName
static string ArrayTypeName(Type type)
{
if (!type.IsArray) return null;
string basename = Get(type.GetElementType());
string rankCommas = new string(',', type.GetArrayRank() - 1);
return basename + "[" + rankCommas + "]";
}
示例5: SetArrayDimensions
void SetArrayDimensions(Type type)
{
if (type.IsArray && type != typeof(Array)) {
SetArrayDimensions(type.GetElementType());
arrays.Insert(0, type.GetArrayRank());
}
}
示例6: AddElementTypes
internal static Type AddElementTypes(SerializationInfo info, Type type)
{
List<int> elementTypes = new List<int>();
while(type.HasElementType)
{
if (type.IsSzArray)
{
elementTypes.Add(SzArray);
}
else if (type.IsArray)
{
elementTypes.Add(type.GetArrayRank());
elementTypes.Add(Array);
}
else if (type.IsPointer)
{
elementTypes.Add(Pointer);
}
else if (type.IsByRef)
{
elementTypes.Add(ByRef);
}
type = type.GetElementType();
}
info.AddValue("ElementTypes", elementTypes.ToArray(), typeof(int[]));
return type;
}
示例7: Array_To_IList
//
// From a one-dimensional array-type S[] to System.Collections.IList<T> and base
// interfaces of this interface, provided there is an implicit reference conversion
// from S to T.
//
static bool Array_To_IList (Type array, Type list, bool isExplicit)
{
if ((array.GetArrayRank () != 1) || !TypeManager.IsGenericType (list))
return false;
Type gt = TypeManager.DropGenericTypeArguments (list);
if ((gt != TypeManager.generic_ilist_type) &&
(gt != TypeManager.generic_icollection_type) &&
(gt != TypeManager.generic_ienumerable_type))
return false;
Type element_type = TypeManager.GetElementType (array);
Type arg_type = TypeManager.TypeToCoreType (TypeManager.GetTypeArguments (list) [0]);
if (element_type == arg_type)
return true;
if (isExplicit)
return ExplicitReferenceConversionExists (element_type, arg_type);
Type t = TypeManager.GetElementType (array);
if (MyEmptyExpr == null)
MyEmptyExpr = new EmptyExpression (t);
else
MyEmptyExpr.SetType (t);
return ImplicitReferenceConversionExists (MyEmptyExpr, arg_type);
}
示例8: Aggregate
/// <summary>
/// Get a function that coerces a sequence of one type into another type.
/// This is primarily used for aggregators stored in ProjectionExpression's,
/// which are used to represent the final transformation of the entire result set of a query.
/// </summary>
public static LambdaExpression Aggregate(Type expectedType, Type actualType)
{
Type actualElementType = actualType.GetSequenceElementType();
if (!expectedType.IsAssignableFrom(actualType))
{
Type expectedElementType = expectedType.GetSequenceElementType();
ParameterExpression p = Expression.Parameter(actualType, "p");
Expression body = null;
if (expectedType.IsAssignableFrom(actualElementType))
body = Expression.Call(typeof(Enumerable), "SingleOrDefault", new Type[] { actualElementType }, p);
else if (expectedType.IsGenericType && expectedType.GetGenericTypeDefinition() == typeof(IQueryable<>))
body = Expression.Call(typeof(Queryable), "AsQueryable", new Type[] { expectedElementType },
CoerceElement(expectedElementType, p));
else if (expectedType.IsArray && expectedType.GetArrayRank() == 1)
body = Expression.Call(typeof(Enumerable), "ToArray", new Type[] { expectedElementType },
CoerceElement(expectedElementType, p));
else if (expectedType.IsAssignableFrom(typeof(List<>).MakeGenericType(actualElementType)))
body = Expression.Call(typeof(Enumerable), "ToList", new Type[] { expectedElementType },
CoerceElement(expectedElementType, p));
else
{
ConstructorInfo ci = expectedType.GetConstructor(new Type[] { actualType });
if (ci != null)
body = Expression.New(ci, p);
}
if (body != null)
return Expression.Lambda(body, p);
}
return null;
}
示例9: CreateSpecialTypeDefOrNull
TypeDef CreateSpecialTypeDefOrNull(Type type)
{
BuiltInTypeDef typeDef = null;
if (typeof(IEnumerable).IsAssignableFrom(type))
{
if (type.IsArray && type.GetArrayRank() == 1)
{
var elementType = type.GetElementType();
var typeDefOfContainedType = GetOrCreateTypeDef(new QualifiedClassName(elementType), elementType);
typeDef = new BuiltInTypeDef(type, "", string.Format("{0}[]", typeDefOfContainedType.FullyQualifiedTsTypeName));
}
else if (!type.IsGenericType)
{
typeDef = new BuiltInTypeDef(type, "", "any[]");
}
else if (type.IsGenericType && type.GetGenericArguments().Length == 1)
{
var elementType = type.GetGenericArguments()[0];
var typeDefOfContainedType = GetOrCreateTypeDef(new QualifiedClassName(elementType), elementType);
typeDef = new BuiltInTypeDef(type, "", string.Format("{0}[]", typeDefOfContainedType.FullyQualifiedTsTypeName));
}
}
if (typeDef != null)
{
_types.Add(type, typeDef);
}
return typeDef;
}
示例10: FormalTypeName
public FormalTypeName(Type type)
{
if (type.IsArray)
{
_arrayRank = type.GetArrayRank();
_arrayElementType = new FormalTypeName(type.GetElementType());
}
_isGenericTypeArg = type.IsGenericParameter;
_isRef = !type.IsValueType;
_isByRef = type.IsByRef;
_name = type.Name;
if (_isByRef && _name.EndsWith("&"))
_name = _name.Remove(_name.Length - 1);
_namespace = type.Namespace;
if (type.IsNested && type.FullName != null)
{
_wrapperClass = new FormalTypeName(type.DeclaringType);
}
if (type.IsGenericType)
{
if (type.IsGenericTypeDefinition)
{
ReadGenericDefinition(type);
}
else
{
ReadGenericDefinition(type.GetGenericTypeDefinition());
var args = type.GetGenericArguments();
_genericInstance = new FormalTypeName[args.Length];
for (var i = 0; i < args.Length; i++)
_genericInstance[i] = new FormalTypeName(args[i]);
}
}
}
示例11: AddElementTypes
internal static Type AddElementTypes(SerializationInfo info, Type type)
{
List<int> list = new List<int>();
while (type.HasElementType)
{
if (type.IsSzArray)
{
list.Add(3);
}
else if (type.IsArray)
{
list.Add(type.GetArrayRank());
list.Add(2);
}
else if (type.IsPointer)
{
list.Add(1);
}
else if (type.IsByRef)
{
list.Add(4);
}
type = type.GetElementType();
}
info.AddValue("ElementTypes", list.ToArray(), typeof(int[]));
return type;
}
示例12: ReadJson
/// <summary>
/// Reads the JSON representation of the object.
/// </summary>
/// <param name="reader">The <see cref="T:Newtonsoft.Json.JsonReader"/> to read from.</param>
/// <param name="objectType">Type of the object.</param>
/// <param name="existingValue">The existing value of object being read.</param>
/// <param name="serializer">The calling serializer.</param>
/// <returns>The object value.</returns>
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
// Handle null values
if (reader.TokenType == JsonToken.Null) return null;
var arrayItemType = objectType.GetElementType();
var arrayRank = objectType.GetArrayRank();
// Retrieve all the values from the Json
var arrayValues = ReadRank(reader, serializer);
// Determine the lengths of all ranks for the array
var rankLengthList = GetRankLengthList(arrayValues);
// If empty values were found, make sure the ranks match in size
for (var i = rankLengthList.Count; i < arrayRank; i++)
{
rankLengthList.Add(0);
}
var rankLengthArray = rankLengthList.ToArray();
// Create the array that will hold the values
var retVal = Array.CreateInstance(arrayItemType, rankLengthArray);
// Make the assignments
SetValues(retVal, rankLengthArray, new int[0], 0, arrayValues);
return retVal;
}
示例13: ArrayDecorator
public ArrayDecorator(IProtoSerializer tail, int fieldNumber, bool writePacked, WireType packedWireType, Type arrayType, bool overwriteList, bool supportNull)
: base(tail)
{
Helpers.DebugAssert(arrayType != null, "arrayType should be non-null");
Helpers.DebugAssert(arrayType.IsArray && arrayType.GetArrayRank() == 1, "should be single-dimension array; " + arrayType.FullName);
this.itemType = arrayType.GetElementType();
#if NO_GENERICS
Type underlyingItemType = itemType;
#else
Type underlyingItemType = supportNull ? itemType : (Nullable.GetUnderlyingType(itemType) ?? itemType);
#endif
Helpers.DebugAssert(underlyingItemType == Tail.ExpectedType, "invalid tail");
Helpers.DebugAssert(Tail.ExpectedType != typeof(byte), "Should have used BlobSerializer");
if ((writePacked || packedWireType != WireType.None) && fieldNumber <= 0) throw new ArgumentOutOfRangeException("fieldNumber");
if (!ListDecorator.CanPack(packedWireType))
{
if (writePacked) throw new InvalidOperationException("Only simple data-types can use packed encoding");
packedWireType = WireType.None;
}
this.fieldNumber = fieldNumber;
this.packedWireType = packedWireType;
if (writePacked) options |= OPTIONS_WritePacked;
if (overwriteList) options |= OPTIONS_OverwriteList;
if (supportNull) options |= OPTIONS_SupportNull;
this.arrayType = arrayType;
}
示例14: GetWriteJsonMethod
internal static WriteJsonValue GetWriteJsonMethod(Type type)
{
var t = Reflection.GetJsonDataType (type);
if (t == JsonDataType.Primitive) {
return typeof (decimal).Equals (type) ? WriteDecimal
: typeof (byte).Equals (type) ? WriteByte
: typeof (sbyte).Equals (type) ? WriteSByte
: typeof (short).Equals (type) ? WriteInt16
: typeof (ushort).Equals (type) ? WriteUInt16
: typeof (uint).Equals (type) ? WriteUInt32
: typeof (ulong).Equals (type) ? WriteUInt64
: typeof (char).Equals (type) ? WriteChar
: (WriteJsonValue)WriteUnknown;
}
else if (t == JsonDataType.Undefined) {
return type.IsSubclassOf (typeof (Array)) && type.GetArrayRank () > 1 ? WriteMultiDimensionalArray
: type.IsSubclassOf (typeof (Array)) && typeof (byte[]).Equals (type) == false ? WriteArray
: typeof (KeyValuePair<string,object>).Equals (type) ? WriteKeyObjectPair
: typeof (KeyValuePair<string,string>).Equals (type) ? WriteKeyValuePair
: (WriteJsonValue)WriteObject;
}
else {
return _convertMethods[(int)t];
}
}
示例15: AppendPrettyNameCore
private static StringBuilder AppendPrettyNameCore(this StringBuilder name, Type type, PrettyNameContext context)
{
// Suffixes (array, ref, pointer)
if (type.IsArray)
return name
.AppendPrettyName(type.GetElementType(), context)
.Append('[')
.Append(',', type.GetArrayRank() - 1)
.Append(']');
else if (type.IsByRef)
return name
.AppendPrettyName(type.GetElementType(), context)
.Append('&');
else if (type.IsPointer)
return name
.AppendPrettyName(type.GetElementType(), context)
.Append('*');
// Prefixes (nesting, namespace)
if (type.IsNested)
name.AppendPrettyNameCore(type.DeclaringType, context)
.Append('.');
else if (context.IsQualified)
name.Append(type.Namespace)
.Append('.');
// Name and arguments
if (type.IsGenericType)
return name
.Append(type.Name.WithoutGenericSuffix())
.AppendPrettyArguments(type, context);
else
return name
.Append(type.Name);
}