98 lines
3.4 KiB
C#
98 lines
3.4 KiB
C#
using System;
|
|
using System.Collections.ObjectModel;
|
|
using System.Reflection;
|
|
using System.Reflection.Emit;
|
|
|
|
namespace System.Linq.Expressions
|
|
{
|
|
/// <summary>Represents creating a new array and possibly initializing the elements of the new array.</summary>
|
|
// Token: 0x0200002C RID: 44
|
|
public sealed class NewArrayExpression : Expression
|
|
{
|
|
// Token: 0x06000239 RID: 569 RVA: 0x0000BD5C File Offset: 0x00009F5C
|
|
internal NewArrayExpression(ExpressionType et, Type type, ReadOnlyCollection<Expression> expressions)
|
|
: base(et, type)
|
|
{
|
|
this.expressions = expressions;
|
|
}
|
|
|
|
/// <summary>Gets the bounds of the array if the value of the <see cref="P:System.Linq.Expressions.Expression.NodeType" /> property is <see cref="F:System.Linq.Expressions.ExpressionType.NewArrayBounds" />, or the values to initialize the elements of the new array if the value of the <see cref="P:System.Linq.Expressions.Expression.NodeType" /> property is <see cref="F:System.Linq.Expressions.ExpressionType.NewArrayInit" />.</summary>
|
|
/// <returns>A <see cref="T:System.Collections.ObjectModel.ReadOnlyCollection`1" /> of <see cref="T:System.Linq.Expressions.Expression" /> objects which represent either the bounds of the array or the initialization values.</returns>
|
|
// Token: 0x1700002F RID: 47
|
|
// (get) Token: 0x0600023A RID: 570 RVA: 0x0000BD70 File Offset: 0x00009F70
|
|
public ReadOnlyCollection<Expression> Expressions
|
|
{
|
|
get
|
|
{
|
|
return this.expressions;
|
|
}
|
|
}
|
|
|
|
// Token: 0x0600023B RID: 571 RVA: 0x0000BD78 File Offset: 0x00009F78
|
|
private void EmitNewArrayInit(EmitContext ec, Type type)
|
|
{
|
|
int count = this.expressions.Count;
|
|
ec.ig.Emit(OpCodes.Ldc_I4, count);
|
|
ec.ig.Emit(OpCodes.Newarr, type);
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
ec.ig.Emit(OpCodes.Dup);
|
|
ec.ig.Emit(OpCodes.Ldc_I4, i);
|
|
this.expressions[i].Emit(ec);
|
|
ec.ig.Emit(OpCodes.Stelem, type);
|
|
}
|
|
}
|
|
|
|
// Token: 0x0600023C RID: 572 RVA: 0x0000BE0C File Offset: 0x0000A00C
|
|
private void EmitNewArrayBounds(EmitContext ec, Type type)
|
|
{
|
|
int count = this.expressions.Count;
|
|
ec.EmitCollection<Expression>(this.expressions);
|
|
if (count == 1)
|
|
{
|
|
ec.ig.Emit(OpCodes.Newarr, type);
|
|
return;
|
|
}
|
|
ec.ig.Emit(OpCodes.Newobj, NewArrayExpression.GetArrayConstructor(type, count));
|
|
}
|
|
|
|
// Token: 0x0600023D RID: 573 RVA: 0x0000BE64 File Offset: 0x0000A064
|
|
private static ConstructorInfo GetArrayConstructor(Type type, int rank)
|
|
{
|
|
return NewArrayExpression.CreateArray(type, rank).GetConstructor(NewArrayExpression.CreateTypeParameters(rank));
|
|
}
|
|
|
|
// Token: 0x0600023E RID: 574 RVA: 0x0000BE78 File Offset: 0x0000A078
|
|
private static Type[] CreateTypeParameters(int rank)
|
|
{
|
|
return Enumerable.Repeat<Type>(typeof(int), rank).ToArray<Type>();
|
|
}
|
|
|
|
// Token: 0x0600023F RID: 575 RVA: 0x0000BE90 File Offset: 0x0000A090
|
|
private static Type CreateArray(Type type, int rank)
|
|
{
|
|
return type.MakeArrayType(rank);
|
|
}
|
|
|
|
// Token: 0x06000240 RID: 576 RVA: 0x0000BE9C File Offset: 0x0000A09C
|
|
internal override void Emit(EmitContext ec)
|
|
{
|
|
Type elementType = base.Type.GetElementType();
|
|
ExpressionType nodeType = base.NodeType;
|
|
if (nodeType == ExpressionType.NewArrayInit)
|
|
{
|
|
this.EmitNewArrayInit(ec, elementType);
|
|
return;
|
|
}
|
|
if (nodeType != ExpressionType.NewArrayBounds)
|
|
{
|
|
throw new NotSupportedException();
|
|
}
|
|
this.EmitNewArrayBounds(ec, elementType);
|
|
}
|
|
|
|
// Token: 0x040000B9 RID: 185
|
|
private ReadOnlyCollection<Expression> expressions;
|
|
}
|
|
}
|