Files
PowerShell-PowerShell/src/System.Management.Automation/engine/ComInterop/VariantArray.cs
T
Steve Lee 4af3068566 Code cleanup: Add space after closing brace where needed (#8530)
* Update TestService

* Update WebListener

* Update Controllers

* Update ExpTest

* Update MyApp

* Update Logic

* Update Logic

* Update MyApp

* Update Microsoft.Management.Infrastructure.CimCmdlets

* Update Microsoft.PowerShell.Commands.Diagnostics

* Update Microsoft.PowerShell.ScheduledJob

* Update Microsoft.WSMan.Management

* Update Microsoft.WSMan.Runtime

* Update ResGen

* Update TypeCatalogGen

* Update commands

* Update Eventing

* Update Reader

* Update utility

* Update ShowCommand

* Update trace

* Update WebCmdlet

* Update Common

* Update CoreCLR

* Update common

* Update format-object

* Update format-wide

* Update out-file

* Update out-printer

* Update out-string

* Update OutGridView

* Update LocalAccounts

* Update Commands

* Update security

* Update CoreCLR

* Update DscSupport

* Update engine

* Update help

* Update logging

* Update namespaces

* Update security

* Update utils

* Update config

* Update perfCounters

* Update tracing

* Update cmdletization

* Update other

* Update cim

* Update xml

* Update CoreCLR

* Update common

* Update DefaultFormatters

* Update out-console

* Update out-textInterface

* Update DisplayDatabase

* Update Utilities

* Update COM

* Update ComInterop

* Update CommandCompletion

* Update debugger

* Update ExperimentalFeature

* Update hostifaces

* Update interpreter

* Update lang

* Update Modules

* Update parser

* Update runtime

* Update client

* Update commands

* Update common

* Update fanin

* Update server

* Update WireDataFormat

* Update Binding

* Update Operations

* Update interface

* Update cmdletization

* Update cim

* Update management

* Update WindowsTaskbarJumpList

* Update msh
2018-12-24 11:20:06 +05:00

111 lines
3.4 KiB
C#

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#if !SILVERLIGHT // ComObject
#if !CLR2
using System.Linq.Expressions;
#else
using Microsoft.Scripting.Ast;
#endif
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.InteropServices;
namespace System.Management.Automation.ComInterop
{
[StructLayout(LayoutKind.Sequential)]
internal struct VariantArray1
{
public Variant Element0;
}
[StructLayout(LayoutKind.Sequential)]
internal struct VariantArray2
{
public Variant Element0, Element1;
}
[StructLayout(LayoutKind.Sequential)]
internal struct VariantArray4
{
public Variant Element0, Element1, Element2, Element3;
}
[StructLayout(LayoutKind.Sequential)]
internal struct VariantArray8
{
public Variant Element0, Element1, Element2, Element3, Element4, Element5, Element6, Element7;
}
//
// Helper for getting the right VariantArray struct for a given number of
// arguments. Will generate a struct if needed.
//
// We use this because we don't have stackalloc or pinning in Expression
// Trees, so we can't create an array of Variants directly.
//
internal static class VariantArray
{
// Don't need a dictionary for this, it will have very few elements
// (guaranteed less than 28, in practice 0-2)
private static readonly List<Type> s_generatedTypes = new List<Type>(0);
internal static MemberExpression GetStructField(ParameterExpression variantArray, int field)
{
return Expression.Field(variantArray, "Element" + field);
}
internal static Type GetStructType(int args)
{
Debug.Assert(args >= 0);
if (args <= 1) return typeof(VariantArray1);
if (args <= 2) return typeof(VariantArray2);
if (args <= 4) return typeof(VariantArray4);
if (args <= 8) return typeof(VariantArray8);
int size = 1;
while (args > size)
{
size *= 2;
}
lock (s_generatedTypes)
{
// See if we can find an existing type
foreach (Type t in s_generatedTypes)
{
int arity = int.Parse(t.Name.Substring("VariantArray".Length), CultureInfo.InvariantCulture);
if (size == arity)
{
return t;
}
}
// Else generate a new type
Type type = CreateCustomType(size).MakeGenericType(new Type[] { typeof(Variant) });
s_generatedTypes.Add(type);
return type;
}
}
private static Type CreateCustomType(int size)
{
var attrs = TypeAttributes.NotPublic | TypeAttributes.SequentialLayout;
TypeBuilder type = UnsafeMethods.DynamicModule.DefineType("VariantArray" + size, attrs, typeof(ValueType));
var T = type.DefineGenericParameters(new string[] { "T" })[0];
for (int i = 0; i < size; i++)
{
type.DefineField("Element" + i, T, FieldAttributes.Public);
}
return type.CreateType();
}
}
}
#endif