本文整理汇总了C#中Type.MakeArrayType方法的典型用法代码示例。如果您正苦于以下问题:C# Type.MakeArrayType方法的具体用法?C# Type.MakeArrayType怎么用?C# Type.MakeArrayType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Type
的用法示例。
在下文中一共展示了Type.MakeArrayType方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestMakeArrayType
public static void TestMakeArrayType(Type t, Type tArrayExpected)
{
Type tArray = t.MakeArrayType();
Assert.Equal(tArrayExpected, tArray);
Assert.Equal(t, tArray.GetElementType());
Assert.True(tArray.IsArray);
Assert.True(tArray.HasElementType);
string s1 = t.ToString();
string s2 = tArray.ToString();
Assert.Equal(s2, s1 + "[]");
}
示例2: NewArrayBounds
/// <summary>
/// Creates a <see cref="NewArrayExpression"/> that represents creating an array that has a specified rank.
/// </summary>
/// <param name="type">A <see cref="System.Type"/> that represents the element type of the array.</param>
/// <param name="bounds">An <see cref="IEnumerable{T}"/> that contains <see cref="Expression"/> objects to use to populate the <see cref="NewArrayExpression.Expressions"/> collection.</param>
/// <returns>A <see cref="NewArrayExpression"/> that has the <see cref="NodeType"/> property equal to <see cref="ExpressionType.NewArrayBounds"/> and the <see cref="NewArrayExpression.Expressions"/> property set to the specified value.</returns>
public static NewArrayExpression NewArrayBounds(Type type, IEnumerable<Expression> bounds)
{
ContractUtils.RequiresNotNull(type, nameof(type));
ContractUtils.RequiresNotNull(bounds, nameof(bounds));
if (type == typeof(void))
{
throw Error.ArgumentCannotBeOfTypeVoid(nameof(type));
}
TypeUtils.ValidateType(type, nameof(type));
if (type.IsByRef)
{
throw Error.TypeMustNotBeByRef(nameof(type));
}
if (type.IsPointer)
{
throw Error.TypeMustNotBePointer(nameof(type));
}
ReadOnlyCollection<Expression> boundsList = bounds.ToReadOnly();
int dimensions = boundsList.Count;
if (dimensions <= 0) throw Error.BoundsCannotBeLessThanOne(nameof(bounds));
for (int i = 0; i < dimensions; i++)
{
Expression expr = boundsList[i];
RequiresCanRead(expr, nameof(bounds), i);
if (!expr.Type.IsInteger())
{
throw Error.ArgumentMustBeInteger(nameof(bounds), i);
}
}
Type arrayType;
if (dimensions == 1)
{
//To get a vector, need call Type.MakeArrayType().
//Type.MakeArrayType(1) gives a non-vector array, which will cause type check error.
arrayType = type.MakeArrayType();
}
else
{
arrayType = type.MakeArrayType(dimensions);
}
return NewArrayExpression.Make(ExpressionType.NewArrayBounds, arrayType, bounds.ToReadOnly());
}
示例3: NewArrayInit
/// <summary>
/// Creates a <see cref="NewArrayExpression"/> of the specified type from the provided initializers.
/// </summary>
/// <param name="type">A Type that represents the element type of the array.</param>
/// <param name="initializers">The expressions used to create the array elements.</param>
/// <returns>A <see cref="NewArrayExpression"/> that has the <see cref="NodeType"/> property equal to <see cref="ExpressionType.NewArrayInit"/> and the <see cref="NewArrayExpression.Expressions"/> property set to the specified value.</returns>
public static NewArrayExpression NewArrayInit(Type type, IEnumerable<Expression> initializers)
{
ContractUtils.RequiresNotNull(type, nameof(type));
ContractUtils.RequiresNotNull(initializers, nameof(initializers));
if (type == typeof(void))
{
throw Error.ArgumentCannotBeOfTypeVoid(nameof(type));
}
TypeUtils.ValidateType(type, nameof(type));
if (type.IsByRef)
{
throw Error.TypeMustNotBeByRef(nameof(type));
}
if (type.IsPointer)
{
throw Error.TypeMustNotBePointer(nameof(type));
}
ReadOnlyCollection<Expression> initializerList = initializers.ToReadOnly();
Expression[] newList = null;
for (int i = 0, n = initializerList.Count; i < n; i++)
{
Expression expr = initializerList[i];
RequiresCanRead(expr, nameof(initializers), i);
if (!TypeUtils.AreReferenceAssignable(type, expr.Type))
{
if (!TryQuote(type, ref expr))
{
throw Error.ExpressionTypeCannotInitializeArrayType(expr.Type, type);
}
if (newList == null)
{
newList = new Expression[initializerList.Count];
for (int j = 0; j < i; j++)
{
newList[j] = initializerList[j];
}
}
}
if (newList != null)
{
newList[i] = expr;
}
}
if (newList != null)
{
initializerList = new TrueReadOnlyCollection<Expression>(newList);
}
return NewArrayExpression.Make(ExpressionType.NewArrayInit, type.MakeArrayType(), initializerList);
}
示例4: NewArrayInit
/// <summary>
/// Creates a new array expression of the specified type from the provided initializers.
/// </summary>
/// <param name="type">A Type that represents the element type of the array.</param>
/// <param name="initializers">The expressions used to create the array elements.</param>
public static NewArrayExpression NewArrayInit(Type type, IEnumerable<Expression> initializers) {
ContractUtils.RequiresNotNull(type, "type");
ContractUtils.RequiresNotNull(initializers, "initializers");
if (type.Equals(typeof(void))) {
throw Error.ArgumentCannotBeOfTypeVoid();
}
ReadOnlyCollection<Expression> initializerList = initializers.ToReadOnly();
Expression[] newList = null;
for (int i = 0, n = initializerList.Count; i < n; i++) {
Expression expr = initializerList[i];
RequiresCanRead(expr, "initializers");
if (!TypeUtils.AreReferenceAssignable(type, expr.Type)) {
if (TypeUtils.IsSameOrSubclass(typeof(Expression), type) && type.IsAssignableFrom(expr.GetType())) {
expr = Expression.Quote(expr);
} else {
throw Error.ExpressionTypeCannotInitializeArrayType(expr.Type, type);
}
if (newList == null) {
newList = new Expression[initializerList.Count];
for (int j = 0; j < i; j++) {
newList[j] = initializerList[j];
}
}
}
if (newList != null) {
newList[i] = expr;
}
}
if (newList != null) {
initializerList = new ReadOnlyCollection<Expression>(newList);
}
return NewArrayExpression.Make(ExpressionType.NewArrayInit, type.MakeArrayType(), initializerList);
}
示例5: NewArrayBounds
//CONFORMING
public static NewArrayExpression NewArrayBounds(Type type, IEnumerable<Expression> bounds) {
ContractUtils.RequiresNotNull(type, "type");
ContractUtils.RequiresNotNull(bounds, "bounds");
if (type.Equals(typeof(void))) {
throw Error.ArgumentCannotBeOfTypeVoid();
}
ReadOnlyCollection<Expression> boundsList = bounds.ToReadOnly();
int dimensions = boundsList.Count;
ContractUtils.Requires(dimensions > 0, "bounds", Strings.BoundsCannotBeLessThanOne);
for (int i = 0; i < dimensions; i++) {
Expression expr = boundsList[i];
RequiresCanRead(expr, "bounds");
if (!TypeUtils.IsInteger(expr.Type)) {
throw Error.ArgumentMustBeInteger();
}
}
Type arrayType;
if (dimensions == 1) {
//To get a vector, need call Type.MakeArrayType().
//Type.MakeArrayType(1) gives a non-vector array, which will cause type check error.
arrayType = type.MakeArrayType();
} else {
arrayType = type.MakeArrayType(dimensions);
}
return NewArrayExpression.Make(ExpressionType.NewArrayBounds, arrayType, bounds.ToReadOnly());
}
示例6: CreateArray
private static Type CreateArray(Type type, int rank)
{
return type.MakeArrayType(rank);
}