/Microsoft.Build/Microsoft.Build/Microsoft/Build/Evaluation/Expander!2.cs
C# | 3960 lines | 3951 code | 9 blank | 0 comment | 31 complexity | 76d716ef0e206723446b06fb2f45bbf0 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-3.0
Large files files are truncated, but you can click here to view the full file
- namespace Microsoft.Build.Evaluation
- {
- using Microsoft.Build;
- using Microsoft.Build.Collections;
- using Microsoft.Build.Execution;
- using Microsoft.Build.Internal;
- using Microsoft.Build.Shared;
- using Microsoft.Win32;
- using System;
- using System.Collections;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Globalization;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Runtime;
- using System.Runtime.CompilerServices;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading;
-
- internal class Expander<P, I> where P: class, IProperty where I: class, IItem
- {
- private static readonly bool defaultWarnForUninitializedProperties;
- private static char[] expandableChars;
- private static CompareInfo invariantCompareInfo;
- private IItemProvider<I> items;
- private IMetadataTable metadata;
- private IPropertyProvider<P> properties;
- private Microsoft.Build.Evaluation.UsedUninitializedProperties usedUninitializedProperties;
-
- static Expander()
- {
- Expander<P, I>.defaultWarnForUninitializedProperties = !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("MSBUILDWARNONUNINITIALIZEDPROPERTY"));
- Expander<P, I>.expandableChars = new char[] { '$', '%', '@' };
- Expander<P, I>.invariantCompareInfo = CultureInfo.InvariantCulture.CompareInfo;
- }
-
- internal Expander(IPropertyProvider<P> properties)
- {
- this.properties = properties;
- this.usedUninitializedProperties = new Microsoft.Build.Evaluation.UsedUninitializedProperties();
- }
-
- [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
- internal Expander(IPropertyProvider<P> properties, IItemProvider<I> items) : this(properties)
- {
- this.items = items;
- }
-
- [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
- internal Expander(IPropertyProvider<P> properties, IItemProvider<I> items, IMetadataTable metadata) : this(properties, items)
- {
- this.metadata = metadata;
- }
-
- private static void AddArgument(List<string> arguments, StringBuilder argumentBuilder)
- {
- ErrorUtilities.VerifyThrowArgumentNull(argumentBuilder, "argumentBuilder");
- string strB = OpportunisticIntern.StringBuilderToString(argumentBuilder).Trim();
- if (string.Compare("null", strB, StringComparison.OrdinalIgnoreCase) == 0)
- {
- arguments.Add(null);
- }
- else if (strB.Length > 0)
- {
- if ((strB[0] == '\'') && (strB[strB.Length - 1] == '\''))
- {
- arguments.Add(strB.Trim(new char[] { '\'' }));
- }
- else if ((strB[0] == '`') && (strB[strB.Length - 1] == '`'))
- {
- arguments.Add(strB.Trim(new char[] { '`' }));
- }
- else if ((strB[0] == '"') && (strB[strB.Length - 1] == '"'))
- {
- arguments.Add(strB.Trim(new char[] { '"' }));
- }
- else
- {
- arguments.Add(strB);
- }
- }
- else
- {
- arguments.Add(strB);
- }
- }
-
- internal IList<T> ExpandIntoItemsLeaveEscaped<T>(string expression, IItemFactory<I, T> itemFactory, ExpanderOptions options, IElementLocation elementLocation) where T: class, IItem
- {
- if (expression.Length == 0)
- {
- return ReadOnlyEmptyList<T>.Instance;
- }
- ErrorUtilities.VerifyThrowInternalNull(elementLocation, "elementLocation");
- expression = MetadataExpander<P, I>.ExpandMetadataLeaveEscaped(expression, this.metadata, options);
- expression = PropertyExpander<P, I, P>.ExpandPropertiesLeaveEscaped(expression, this.properties, options, elementLocation, this.usedUninitializedProperties);
- List<T> list = new List<T>();
- if (expression.Length != 0)
- {
- foreach (string str in ExpressionShredder.SplitSemiColonSeparatedList(expression))
- {
- bool flag;
- IList<T> collection = ItemExpander<P, I>.ExpandSingleItemVectorExpressionIntoItems<I, T>((Expander<P, I>) this, str, this.items, itemFactory, options, false, out flag, elementLocation);
- if (((collection == null) || (collection.Count > 0)) && ((options & ExpanderOptions.BreakOnNotEmpty) != ExpanderOptions.Invalid))
- {
- return null;
- }
- if (collection != null)
- {
- list.AddRange(collection);
- }
- else
- {
- T item = itemFactory.CreateItem(str);
- list.Add(item);
- }
- }
- }
- return list;
- }
-
- internal string ExpandIntoStringAndUnescape(string expression, ExpanderOptions options, IElementLocation elementLocation)
- {
- string escapedString = this.ExpandIntoStringLeaveEscaped(expression, options, elementLocation);
- return ((escapedString == null) ? null : EscapingUtilities.UnescapeAll(escapedString));
- }
-
- internal string ExpandIntoStringLeaveEscaped(string expression, ExpanderOptions options, IElementLocation elementLocation)
- {
- if (expression.Length == 0)
- {
- return string.Empty;
- }
- ErrorUtilities.VerifyThrowInternalNull(elementLocation, "elementLocation");
- string str = PropertyExpander<P, I, P>.ExpandPropertiesLeaveEscaped(MetadataExpander<P, I>.ExpandMetadataLeaveEscaped(expression, this.metadata, options), this.properties, options, elementLocation, this.usedUninitializedProperties);
- return ItemExpander<P, I>.ExpandItemVectorsIntoString<I>((Expander<P, I>) this, str, this.items, options, elementLocation);
- }
-
- internal IList<string> ExpandIntoStringListLeaveEscaped(string expression, ExpanderOptions options, IElementLocation elementLocation)
- {
- ErrorUtilities.VerifyThrow((options & ExpanderOptions.BreakOnNotEmpty) == ExpanderOptions.Invalid, "not supported");
- return ExpressionShredder.SplitSemiColonSeparatedList(this.ExpandIntoStringLeaveEscaped(expression, options, elementLocation));
- }
-
- internal IList<ProjectItemInstance.TaskItem> ExpandIntoTaskItemsLeaveEscaped(string expression, ExpanderOptions options, IElementLocation elementLocation)
- {
- return this.ExpandIntoItemsLeaveEscaped<ProjectItemInstance.TaskItem>(expression, (IItemFactory<I, ProjectItemInstance.TaskItem>) ProjectItemInstance.TaskItem.TaskItemFactory.Instance, options, elementLocation);
- }
-
- internal object ExpandPropertiesLeaveTypedAndEscaped(string expression, ExpanderOptions options, IElementLocation elementLocation)
- {
- if (expression.Length == 0)
- {
- return string.Empty;
- }
- ErrorUtilities.VerifyThrowInternalNull(elementLocation, "elementLocation");
- return PropertyExpander<P, I, P>.ExpandPropertiesLeaveTypedAndEscaped(MetadataExpander<P, I>.ExpandMetadataLeaveEscaped(expression, this.metadata, options), this.properties, options, elementLocation, this.usedUninitializedProperties);
- }
-
- internal IList<T> ExpandSingleItemVectorExpressionIntoItems<T>(string expression, IItemFactory<I, T> itemFactory, ExpanderOptions options, bool includeNullItems, out bool isTransformExpression, IElementLocation elementLocation) where T: class, IItem
- {
- if (expression.Length == 0)
- {
- isTransformExpression = false;
- return ReadOnlyEmptyList<T>.Instance;
- }
- ErrorUtilities.VerifyThrowInternalNull(elementLocation, "elementLocation");
- return ItemExpander<P, I>.ExpandSingleItemVectorExpressionIntoItems<I, T>((Expander<P, I>) this, expression, this.items, itemFactory, options, includeNullItems, out isTransformExpression, elementLocation);
- }
-
- internal static bool ExpressionContainsItemVector(string expression)
- {
- return (ExpressionShredder.GetReferencedItemExpressions(expression) != null);
- }
-
- internal static bool ExpressionMayContainExpandableExpressions(string expression)
- {
- return (expression.IndexOfAny(Expander<P, I>.expandableChars) > -1);
- }
-
- private static unsafe string[] ExtractFunctionArguments(IElementLocation elementLocation, string expressionFunction, string argumentsString)
- {
- int length = argumentsString.Length;
- List<string> arguments = new List<string>();
- StringBuilder argumentBuilder = new StringBuilder(length);
- fixed (char* str = ((char*) argumentsString))
- {
- char* chPtr = str;
- for (int i = 0; i < length; i++)
- {
- if (((i < (length - 1)) && (chPtr[i] == '$')) && (chPtr[i + 1] == '('))
- {
- int startIndex = i;
- i += 2;
- i = Expander<P, I>.ScanForClosingParenthesis(argumentsString, i);
- if (i == -1)
- {
- ProjectErrorUtilities.ThrowInvalidProject(elementLocation, "InvalidFunctionPropertyExpression", expressionFunction, AssemblyResources.GetString("InvalidFunctionPropertyExpressionDetailMismatchedParenthesis"));
- }
- argumentBuilder.Append(argumentsString.Substring(startIndex, (i - startIndex) + 1));
- }
- else if (((chPtr[i] == '`') || (chPtr[i] == '"')) || (chPtr[i] == '\''))
- {
- int num4 = i;
- i++;
- i = Expander<P, I>.ScanForClosingQuote(argumentsString[num4], argumentsString, i);
- if (i == -1)
- {
- ProjectErrorUtilities.ThrowInvalidProject(elementLocation, "InvalidFunctionPropertyExpression", expressionFunction, AssemblyResources.GetString("InvalidFunctionPropertyExpressionDetailMismatchedQuote"));
- }
- argumentBuilder.Append(argumentsString.Substring(num4, (i - num4) + 1));
- }
- else if (chPtr[i] == ',')
- {
- Expander<P, I>.AddArgument(arguments, argumentBuilder);
- argumentBuilder.Remove(0, argumentBuilder.Length);
- }
- else
- {
- argumentBuilder.Append(chPtr[i]);
- }
- }
- }
- Expander<P, I>.AddArgument(arguments, argumentBuilder);
- return arguments.ToArray();
- }
-
- private static bool IsValidPropertyName(string propertyName)
- {
- if ((propertyName.Length == 0) || !XmlUtilities.IsValidInitialElementNameCharacter(propertyName[0]))
- {
- return false;
- }
- for (int i = 1; i < propertyName.Length; i++)
- {
- if (!XmlUtilities.IsValidSubsequentElementNameCharacter(propertyName[i]))
- {
- return false;
- }
- }
- return true;
- }
-
- private static int ScanForClosingParenthesis(string expression, int index)
- {
- bool potentialPropertyFunction = false;
- bool potentialRegistryFunction = false;
- return Expander<P, I>.ScanForClosingParenthesis(expression, index, out potentialPropertyFunction, out potentialRegistryFunction);
- }
-
- private static unsafe int ScanForClosingParenthesis(string expression, int index, out bool potentialPropertyFunction, out bool potentialRegistryFunction)
- {
- int num = 1;
- int length = expression.Length;
- potentialPropertyFunction = false;
- potentialRegistryFunction = false;
- fixed (char* str = ((char*) expression))
- {
- char* chPtr = str;
- while ((index < length) && (num > 0))
- {
- char quoteChar = chPtr[index];
- switch (quoteChar)
- {
- case '\'':
- case '`':
- case '"':
- index++;
- index = Expander<P, I>.ScanForClosingQuote(quoteChar, expression, index);
- if (index < 0)
- {
- return -1;
- }
- break;
-
- case '(':
- num++;
- break;
-
- case ')':
- num--;
- break;
-
- case '.':
- case '[':
- case '$':
- potentialPropertyFunction = true;
- break;
-
- default:
- if (quoteChar == ':')
- {
- potentialRegistryFunction = true;
- }
- break;
- }
- index++;
- }
- }
- index--;
- if (num != 0)
- {
- return -1;
- }
- return index;
- }
-
- private static unsafe int ScanForClosingQuote(char quoteChar, string expression, int index)
- {
- fixed (char* str = ((char*) expression))
- {
- char* chPtr = str;
- while (index < expression.Length)
- {
- if (chPtr[index] == quoteChar)
- {
- return index;
- }
- index++;
- }
- }
- return -1;
- }
-
- internal IMetadataTable Metadata
- {
- [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
- get
- {
- return this.metadata;
- }
- [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
- set
- {
- this.metadata = value;
- }
- }
-
- internal Microsoft.Build.Evaluation.UsedUninitializedProperties UsedUninitializedProperties
- {
- [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
- get
- {
- return this.usedUninitializedProperties;
- }
- [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
- set
- {
- this.usedUninitializedProperties = value;
- }
- }
-
- internal bool WarnForUninitializedProperties
- {
- get
- {
- return this.usedUninitializedProperties.Warn;
- }
- set
- {
- this.usedUninitializedProperties.Warn = value;
- }
- }
-
- private class Function<T> where T: class, IProperty
- {
- private string[] arguments;
- private BindingFlags bindingFlags;
- private string expression;
- private string expressionRootName;
- private string name;
- private Type objectType;
- private string remainder;
- private UsedUninitializedProperties usedUninitializedProperties;
-
- internal Function(Type objectType, string expression, string expressionRootName, string name, string[] arguments, BindingFlags bindingFlags, string remainder, UsedUninitializedProperties usedUninitializedProperties)
- {
- this.name = name;
- if (arguments == null)
- {
- this.arguments = new string[0];
- }
- else
- {
- this.arguments = arguments;
- }
- this.expressionRootName = expressionRootName;
- this.expression = expression;
- this.objectType = objectType;
- this.bindingFlags = bindingFlags;
- this.remainder = remainder;
- this.usedUninitializedProperties = usedUninitializedProperties;
- }
-
- private static object[] CoerceArguments(object[] args, ParameterInfo[] parameters)
- {
- object[] objArray = new object[args.Length];
- try
- {
- for (int i = 0; i < parameters.Length; i++)
- {
- if (args[i] != null)
- {
- if (parameters[i].ParameterType == typeof(char[]))
- {
- objArray[i] = args[i].ToString().ToCharArray();
- }
- else if ((parameters[i].ParameterType.IsEnum && (args[i] is string)) && ((string) args[i]).Contains("."))
- {
- Type parameterType = parameters[i].ParameterType;
- string oldValue = parameterType.Name + ".";
- string str2 = parameterType.FullName + ".";
- string str3 = args[i].ToString().Replace('|', ',').Replace(str2, "").Replace(oldValue, "");
- objArray[i] = Enum.Parse(parameterType, str3);
- }
- else
- {
- objArray[i] = Convert.ChangeType(args[i], parameters[i].ParameterType, CultureInfo.InvariantCulture);
- }
- }
- }
- }
- catch (InvalidCastException)
- {
- return null;
- }
- return objArray;
- }
-
- private static Expander<P, I>.Function<T> ConstructFunction(IElementLocation elementLocation, string expressionFunction, string expressionRootName, Type objectType, int argumentStartIndex, int methodStartIndex, UsedUninitializedProperties usedUninitializedProperties)
- {
- string[] strArray;
- string str;
- string str2 = string.Empty;
- BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.IgnoreCase;
- if ((argumentStartIndex > -1) && !expressionFunction.Substring(methodStartIndex, argumentStartIndex - methodStartIndex).Contains("."))
- {
- string str3;
- str = expressionFunction.Substring(methodStartIndex, argumentStartIndex - methodStartIndex).Trim();
- argumentStartIndex++;
- int num = Expander<P, I>.ScanForClosingParenthesis(expressionFunction, argumentStartIndex);
- ErrorUtilities.VerifyThrow(num != -1, "Unmatched braces when constructing function.", "$(" + expressionFunction + ")");
- bindingFlags |= BindingFlags.InvokeMethod;
- if (argumentStartIndex == (expressionFunction.Length - 1))
- {
- str3 = string.Empty;
- strArray = new string[0];
- }
- else
- {
- str3 = expressionFunction.Substring(argumentStartIndex, num - argumentStartIndex);
- if (string.IsNullOrEmpty(str3))
- {
- strArray = new string[0];
- }
- else
- {
- strArray = Expander<P, I>.ExtractFunctionArguments(elementLocation, expressionFunction, str3);
- }
- str2 = expressionFunction.Substring(num + 1);
- }
- }
- else
- {
- int index = expressionFunction.IndexOf('.', methodStartIndex);
- int length = expressionFunction.Length - methodStartIndex;
- int num4 = expressionFunction.IndexOf('[', methodStartIndex);
- if ((num4 >= 0) && (num4 < index))
- {
- index = num4;
- }
- strArray = new string[0];
- if (index > 0)
- {
- length = index - methodStartIndex;
- str2 = expressionFunction.Substring(index);
- }
- string str4 = expressionFunction.Substring(methodStartIndex, length).Trim();
- ProjectErrorUtilities.VerifyThrowInvalidProject(str4.Length > 0, elementLocation, "InvalidFunctionPropertyExpression", expressionFunction, string.Empty);
- bindingFlags |= BindingFlags.GetProperty | BindingFlags.GetField;
- str = str4;
- }
- if ((string.IsNullOrEmpty(str2) || (str2[0] == '.')) || (str2[0] == '['))
- {
- return new Expander<P, I>.Function<T>(objectType, expressionFunction, expressionRootName, str, strArray, bindingFlags, str2, usedUninitializedProperties);
- }
- ProjectErrorUtilities.ThrowInvalidProject(elementLocation, "InvalidFunctionPropertyExpression", expressionFunction, string.Empty);
- return null;
- }
-
- private static Expander<P, I>.Function<T> ConstructIndexerFunction(string expressionFunction, IElementLocation elementLocation, object propertyValue, string propertyName, Type objectType, int methodStartIndex, string argumentsContent, UsedUninitializedProperties usedUnInitializedProperties)
- {
- string str2;
- string[] strArray;
- string remainder = expressionFunction.Substring(methodStartIndex);
- if (string.IsNullOrEmpty(argumentsContent))
- {
- strArray = new string[0];
- }
- else
- {
- strArray = Expander<P, I>.ExtractFunctionArguments(elementLocation, expressionFunction, argumentsContent);
- }
- if (propertyValue is Array)
- {
- str2 = "GetValue";
- }
- else if (propertyValue is string)
- {
- str2 = "get_Chars";
- }
- else
- {
- str2 = "get_Item";
- }
- return new Expander<P, I>.Function<T>(objectType, expressionFunction, propertyName, str2, strArray, BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.IgnoreCase, remainder, usedUnInitializedProperties);
- }
-
- internal object Execute(object objectInstance, IPropertyProvider<T> properties, ExpanderOptions options, IElementLocation elementLocation)
- {
- object propertyValue = string.Empty;
- object[] args = null;
- try
- {
- if (objectInstance == null)
- {
- if (!this.IsStaticMethodAvailable(this.objectType, this.name))
- {
- ProjectErrorUtilities.ThrowInvalidProject(elementLocation, "InvalidFunctionMethodUnavailable", this.name, this.objectType.FullName);
- }
- this.bindingFlags |= BindingFlags.Static;
- if (this.objectType == typeof(IntrinsicFunctions))
- {
- this.bindingFlags |= BindingFlags.NonPublic;
- }
- }
- else
- {
- this.bindingFlags |= BindingFlags.Instance;
- if (objectInstance is string)
- {
- objectInstance = EscapingUtilities.UnescapeAll((string) objectInstance);
- }
- }
- args = new object[this.arguments.Length];
- for (int i = 0; i < this.arguments.Length; i++)
- {
- object obj3 = Expander<P, I>.PropertyExpander<T>.ExpandPropertiesLeaveTypedAndEscaped(this.arguments[i], properties, options, elementLocation, this.usedUninitializedProperties);
- string escapedString = obj3 as string;
- if (escapedString != null)
- {
- args[i] = EscapingUtilities.UnescapeAll(escapedString);
- }
- else
- {
- args[i] = obj3;
- }
- }
- if (((objectInstance != null) && (args.Length == 1)) && (string.Equals("Equals", this.name, StringComparison.OrdinalIgnoreCase) || string.Equals("CompareTo", this.name, StringComparison.OrdinalIgnoreCase)))
- {
- args[0] = Convert.ChangeType(args[0], objectInstance.GetType(), CultureInfo.InvariantCulture);
- }
- if (string.Equals("new", this.name, StringComparison.OrdinalIgnoreCase))
- {
- propertyValue = this.LateBindExecute(null, BindingFlags.Public | BindingFlags.Instance, null, args, true);
- }
- else
- {
- try
- {
- propertyValue = this.objectType.InvokeMember(this.name, this.bindingFlags, Type.DefaultBinder, objectInstance, args, CultureInfo.InvariantCulture);
- }
- catch (MissingMethodException exception)
- {
- if ((this.bindingFlags & BindingFlags.InvokeMethod) != BindingFlags.InvokeMethod)
- {
- throw;
- }
- propertyValue = this.LateBindExecute(exception, this.bindingFlags, objectInstance, args, false);
- }
- }
- if (((propertyValue is string) && !string.Equals("Unescape", this.name, StringComparison.OrdinalIgnoreCase)) && !string.Equals("Escape", this.name, StringComparison.OrdinalIgnoreCase))
- {
- propertyValue = EscapingUtilities.Escape((string) propertyValue);
- }
- if (string.IsNullOrEmpty(this.remainder))
- {
- return propertyValue;
- }
- return Expander<P, I>.PropertyExpander<T>.ExpandPropertyBody(this.remainder, propertyValue, properties, options, elementLocation, this.usedUninitializedProperties);
- }
- catch (TargetInvocationException exception2)
- {
- string str2 = this.GenerateStringOfMethodExecuted(this.expression, objectInstance, this.name, args);
- ProjectErrorUtilities.ThrowInvalidProject(elementLocation, "InvalidFunctionPropertyExpression", str2, exception2.InnerException.Message.Replace("\r\n", " "));
- return null;
- }
- catch (Exception exception3)
- {
- if (ExceptionHandling.NotExpectedFunctionException(exception3))
- {
- throw;
- }
- if (Expander<P, I>.invariantCompareInfo.IndexOf(this.expression, "::", CompareOptions.OrdinalIgnoreCase) > -1)
- {
- ProjectErrorUtilities.ThrowInvalidProject(elementLocation, "InvalidFunctionStaticMethodSyntax", this.expression, exception3.Message.Replace("Microsoft.Build.Evaluation.IntrinsicFunctions.", "[MSBuild]::"));
- }
- else
- {
- string str3 = this.GenerateStringOfMethodExecuted(this.expression, objectInstance, this.name, args);
- ProjectErrorUtilities.ThrowInvalidProject(elementLocation, "InvalidFunctionPropertyExpression", str3, exception3.Message);
- }
- return null;
- }
- }
-
- internal static Expander<P, I>.Function<T> ExtractPropertyFunction(string expressionFunction, IElementLocation elementLocation, object propertyValue, UsedUninitializedProperties usedUnInitializedProperties)
- {
- string propertyName = null;
- Type objectType = null;
- string str2 = expressionFunction;
- int index = expressionFunction.IndexOf('(');
- if (index > -1)
- {
- str2 = expressionFunction.Substring(0, index);
- }
- ProjectErrorUtilities.VerifyThrowInvalidProject(!string.IsNullOrEmpty(str2), elementLocation, "InvalidFunctionPropertyExpression", expressionFunction, string.Empty);
- int methodStartIndex = -1;
- if ((propertyValue == null) && (str2[0] == '['))
- {
- int num3 = str2.IndexOf(']', 1);
- if (num3 < 1)
- {
- ProjectErrorUtilities.ThrowInvalidProject(elementLocation, "InvalidFunctionStaticMethodSyntax", expressionFunction, string.Empty);
- }
- string typeName = str2.Substring(1, num3 - 1);
- methodStartIndex = num3 + 1;
- objectType = Expander<P, I>.Function<T>.GetTypeForStaticMethod(typeName);
- if (objectType == null)
- {
- ProjectErrorUtilities.ThrowInvalidProject(elementLocation, "InvalidFunctionTypeUnavailable", expressionFunction, typeName);
- }
- if (((str2.Length > (methodStartIndex + 2)) && (str2[methodStartIndex] == ':')) && (str2[methodStartIndex + 1] == ':'))
- {
- methodStartIndex += 2;
- }
- else
- {
- ProjectErrorUtilities.ThrowInvalidProject(elementLocation, "InvalidFunctionStaticMethodSyntax", expressionFunction, string.Empty);
- }
- }
- else
- {
- if (expressionFunction[0] == '[')
- {
- objectType = propertyValue.GetType();
- int num4 = expressionFunction.IndexOf(']', 1);
- if (num4 < 1)
- {
- ProjectErrorUtilities.ThrowInvalidProject(elementLocation, "InvalidFunctionPropertyExpression", expressionFunction, AssemblyResources.GetString("InvalidFunctionPropertyExpressionDetailMismatchedSquareBrackets"));
- }
- string argumentsContent = expressionFunction.Substring(1, num4 - 1);
- methodStartIndex = num4 + 1;
- return Expander<P, I>.Function<T>.ConstructIndexerFunction(expressionFunction, elementLocation, propertyValue, propertyName, objectType, methodStartIndex, argumentsContent, usedUnInitializedProperties);
- }
- methodStartIndex = str2.IndexOf('.');
- if (methodStartIndex == -1)
- {
- return null;
- }
- methodStartIndex++;
- }
- if (objectType == null)
- {
- int length = str2.IndexOf('.');
- propertyName = str2.Substring(0, length);
- if ((propertyValue == null) && !Expander<P, I>.IsValidPropertyName(propertyName))
- {
- ProjectErrorUtilities.ThrowInvalidProject(elementLocation, "InvalidFunctionPropertyExpression", expressionFunction, string.Empty);
- }
- objectType = typeof(string);
- }
- if (propertyValue != null)
- {
- objectType = propertyValue.GetType();
- }
- return Expander<P, I>.Function<T>.ConstructFunction(elementLocation, expressionFunction, propertyName, objectType, index, methodStartIndex, usedUnInitializedProperties);
- }
-
- private string GenerateStringOfMethodExecuted(string expression, object objectInstance, string name, object[] args)
- {
- string str = string.Empty;
- if (args != null)
- {
- foreach (object obj2 in args)
- {
- if (obj2 == null)
- {
- str = str + "null";
- }
- else
- {
- string str2 = obj2.ToString();
- if ((obj2 is string) && (str2.Length == 0))
- {
- str = str + "''";
- }
- else
- {
- str = str + obj2.ToString();
- }
- }
- str = str + ", ";
- }
- if (str.Length > 2)
- {
- str = str.Substring(0, str.Length - 2);
- }
- }
- if (objectInstance == null)
- {
- string fullName = this.objectType.FullName;
- if (this.objectType == typeof(IntrinsicFunctions))
- {
- fullName = "MSBuild";
- }
- if ((this.bindingFlags & BindingFlags.InvokeMethod) == BindingFlags.InvokeMethod)
- {
- return ("[" + fullName + "]::" + name + "(" + str + ")");
- }
- return ("[" + fullName + "]::" + name);
- }
- string str4 = "\"" + objectInstance + "\"";
- if ((this.bindingFlags & BindingFlags.InvokeMethod) == BindingFlags.InvokeMethod)
- {
- return (str4 + "." + name + "(" + str + ")");
- }
- return (str4 + "." + name);
- }
-
- private static Type GetTypeForStaticMethod(string typeName)
- {
- Type typeFromAssembly;
- Tuple<string, Type> tuple;
- if (FunctionConstants.AvailableStaticMethods.TryGetValue(typeName, out tuple) && (tuple != null))
- {
- ErrorUtilities.VerifyThrow((tuple.Item1 != null) || (tuple.Item2 != null), "Function type information needs either string or type represented.");
- if (tuple.Item2 != null)
- {
- return tuple.Item2;
- }
- if (tuple.Item1 != null)
- {
- typeName = tuple.Item1;
- typeFromAssembly = Type.GetType(typeName, false, true);
- FunctionConstants.AvailableStaticMethods[typeName] = new Tuple<string, Type>(typeName, typeFromAssembly);
- return typeFromAssembly;
- }
- }
- typeFromAssembly = Type.GetType(typeName, false, true);
- if (typeFromAssembly == null)
- {
- if (!(Environment.GetEnvironmentVariable("MSBUILDENABLEALLPROPERTYFUNCTIONS") == "1"))
- {
- return typeFromAssembly;
- }
- if (typeFromAssembly == null)
- {
- typeFromAssembly = Expander<P, I>.Function<T>.GetTypeFromAssembly(typeName, "System");
- }
- if (typeFromAssembly == null)
- {
- typeFromAssembly = Expander<P, I>.Function<T>.GetTypeFromAssembly(typeName, "System.Core");
- }
- if (typeFromAssembly == null)
- {
- typeFromAssembly = Expander<P, I>.Function<T>.GetTypeFromAssemblyUsingNamespace(typeName);
- }
- if (typeFromAssembly != null)
- {
- FunctionConstants.AvailableStaticMethods[typeName] = new Tuple<string, Type>(typeName, typeFromAssembly);
- }
- }
- return typeFromAssembly;
- }
-
- private static Type GetTypeFromAssembly(string typeName, string candidateAssemblyName)
- {
- Type type = null;
- Assembly assembly = Assembly.LoadWithPartialName(candidateAssemblyName);
- if (assembly != null)
- {
- type = assembly.GetType(typeName, false, true);
- }
- return type;
- }
-
- private static Type GetTypeFromAssemblyUsingNamespace(string typeName)
- {
- string str = typeName;
- int length = str.LastIndexOf('.');
- Type typeFromAssembly = null;
- ErrorUtilities.VerifyThrow(length > 0, "Invalid typename: {0}", typeName);
- while (length > 0)
- {
- string candidateAssemblyName = null;
- candidateAssemblyName = str.Substring(0, length);
- typeFromAssembly = Expander<P, I>.Function<T>.GetTypeFromAssembly(typeName, candidateAssemblyName);
- if (typeFromAssembly != null)
- {
- return typeFromAssembly;
- }
- length = candidateAssemblyName.LastIndexOf('.');
- }
- return null;
- }
-
- private bool IsStaticMethodAvailable(Type objectType, string methodName)
- {
- if (objectType == typeof(IntrinsicFunctions))
- {
- return true;
- }
- string key = objectType.FullName + "::" + methodName;
- return (FunctionConstants.AvailableStaticMethods.ContainsKey(objectType.FullName) || (FunctionConstants.AvailableStaticMethods.ContainsKey(key) || (Environment.GetEnvironmentVariable("MSBUILDENABLEALLPROPERTYFUNCTIONS") == "1")));
- }
-
- private object LateBindExecute(Exception ex, BindingFlags bindingFlags, object objectInstance, object[] args, bool isConstructor)
- {
- ParameterInfo[] parameters = null;
- MethodBase[] constructors = null;
- MethodBase base2 = null;
- Type[] types = new Type[this.arguments.Length];
- for (int i = 0; i < this.arguments.Length; i++)
- {
- types[i] = typeof(string);
- }
- if (isConstructor)
- {
- base2 = this.objectType.GetConstructor(bindingFlags, null, types, null);
- }
- else
- {
- base2 = this.objectType.GetMethod(this.name, bindingFlags, null, types, null);
- }
- if (base2 == null)
- {
- if (isConstructor)
- {
- constructors = this.objectType.GetConstructors(bindingFlags);
- }
- else
- {
- constructors = this.objectType.GetMethods(bindingFlags);
- }
- object[] objArray = null;
- foreach (MethodBase base3 in constructors)
- {
- parameters = base3.GetParameters();
- if ((parameters.Length == this.arguments.Length) && (isConstructor || string.Equals(base3.Name, this.name, StringComparison.OrdinalIgnoreCase)))
- {
- objArray = Expander<P, I>.Function<T>.CoerceArguments(args, parameters);
- if (objArray != null)
- {
- base2 = base3;
- args = objArray;
- break;
- }
- }
- }
- }
- object obj2 = null;
- if ((base2 != null) && (args != null))
- {
- if (isConstructor)
- {
- obj2 = ((ConstructorInfo) base2).Invoke(args);
- }
- else
- {
- obj2 = ((MethodInfo) base2).Invoke(objectInstance, args);
- }
- }
- else if (!isConstructor)
- {
- throw ex;
- }
- if ((obj2 == null) && isConstructor)
- {
- throw new TargetInvocationException(new MissingMethodException());
- }
- return obj2;
- }
-
- internal string ExpressionRootName
- {
- [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
- get
- {
- return this.expressionRootName;
- }
- }
- }
-
- private static class ItemExpander
- {
- private static bool ExpandItemVectorMatchIntoStringBuilder<S>(Expander<P, I> expander, ExpressionShredder.ItemExpressionCapture match, IItemProvider<S> items, IElementLocation elementLocation, StringBuilder builder, ExpanderOptions options) where S: class, IItem
- {
- string itemType = match.ItemType;
- string str2 = (match.Separator != null) ? match.Separator : ";";
- ProjectErrorUtilities.VerifyThrowInvalidProject(!string.IsNullOrEmpty(itemType), elementLocation, "InvalidFunctionPropertyExpression");
- ICollection<S> itemsOfType = items.GetItems(itemType);
- if (itemsOfType.Count != 0)
- {
- if (match.Captures == null)
- {
- foreach (S local in itemsOfType)
- {
- if ((local.EvaluatedIncludeEscaped.Length > 0) && ((options & ExpanderOptions.BreakOnNotEmpty) != ExpanderOptions.Invalid))
- {
- return true;
- }
- builder.Append(local.EvaluatedIncludeEscaped);
- builder.Append(str2);
- }
- }
- else
- {
- Stack<TransformFunction<P, I, S>> transformFunctionStack = Expander<P, I>.ItemExpander.PrepareTransformStackFromMatch<S>(elementLocation, match);
- foreach (Tuple<string, S> tuple in Expander<P, I>.ItemExpander.Transform<S>(expander, true, transformFunctionStack, IntrinsicItemFunctions<P, I, S>.GetItemTupleEnumerator(itemsOfType)))
- {
- if (((tuple.Item1 != null) && (tuple.Item1.Length > 0)) && ((options & ExpanderOptions.BreakOnNotEmpty) != ExpanderOptions.Invalid))
- {
- return true;
- }
- if (tuple.Item1 != null)
- {
- builder.Append(tuple.Item1);
- }
- builder.Append(str2);
- }
- }
- if (builder.Length > 0)
- {
- builder.Remove(builder.Length - str2.Length, str2.Length);
- }
- }
- return false;
- }
-
- internal static string ExpandItemVectorsIntoString<T>(Expander<P, I> expander, string expression, IItemProvider<T> items, ExpanderOptions options, IElementLocation elementLocation) where T: class, IItem
- {
- if (((options & ExpanderOptions.ExpandItems) == ExpanderOptions.Invalid) || (expression.Length == 0))
- {
- return expression;
- }
- ErrorUtilities.VerifyThrow(items != null, "Cannot expand items without providing items");
- List<ExpressionShredder.ItemExpressionCapture> referencedItemExpressions = ExpressionShredder.GetReferencedItemExpressions(expression);
- if (referencedItemExpressions == null)
- {
- return expression;
- }
- StringBuilder builder = new StringBuilder();
- int startIndex = 0;
- for (int i = 0; i < referencedItemExpressions.Count; i++)
- {
- if (referencedItemExpressions[i].Index > startIndex)
- {
- if ((options & ExpanderOptions.BreakOnNotEmpty) != ExpanderOptions.Invalid)
- {
- return null;
- }
- builder.Append(expression, startIndex, referencedItemExpressions[i].Index - startIndex);
- }
- if (Expander<P, I>.ItemExpander.ExpandItemVectorMatchIntoStringBuilder<T>(expander, referencedItemExpressions[i], items, elementLocation, builder, options))
- {
- return null;
- }
- startIndex = referencedItemExpressions[i].Index + referencedItemExpressions[i].Length;
- }
- builder.Append(expression, startIndex, expression.Length - startIndex);
- return OpportunisticIntern.StringBuilderToString(builder);
- }
-
- internal static IList<T> ExpandSingleItemVectorExpressionIntoItems<S, T>(Expander<P, I> expander, string expression, IItemProvider<S> items, IItemFactory<S, T> itemFactory, ExpanderOptions options, bool includeNullEntries, out bool isTransformExpression, IElementLocation elementLocation) where S: class, IItem where T: class, IItem
- {
- isTransformExpression = false;
- if (((options & ExpanderOptions.ExpandItems) == ExpanderOptions.Invalid) || (expression.Length == 0))
- {
- return null;
- }
- ErrorUtilities.VerifyThrow(items != null, "Cannot expand items without providing items");
- List<ExpressionShredder.ItemExpressionCapture> referencedItemExpressions = null;
- if (Expander<P, I>.invariantCompareInfo.IndexOf(expression, '@') == -1)
- {
- return null;
- }
- referencedItemExpressions = ExpressionShredder.GetReferencedItemExpressions(expression);
- if (referencedItemExpressions == null)
- {
- return null;
- }
- ExpressionShredder.ItemExpressionCapture match = refere…
Large files files are truncated, but you can click here to view the full file