本文整理汇总了C#中Stack.Aggregate方法的典型用法代码示例。如果您正苦于以下问题:C# Stack.Aggregate方法的具体用法?C# Stack.Aggregate怎么用?C# Stack.Aggregate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Stack
的用法示例。
在下文中一共展示了Stack.Aggregate方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetExpressionText
public static string GetExpressionText(LambdaExpression expression) {
// Crack the expression string for property/field accessors to create its name
Stack<string> nameParts = new Stack<string>();
Expression part = expression.Body;
while (part != null) {
if (part.NodeType == System.Linq.Expressions.ExpressionType.MemberAccess) {
MemberExpression memberExpressionPart = (MemberExpression)part;
nameParts.Push(memberExpressionPart.Member.Name);
part = memberExpressionPart.Expression;
}
else {
break;
}
}
// If it starts with "model", then strip that away
if (nameParts.Count > 0 && String.Equals(nameParts.Peek(), "model", StringComparison.OrdinalIgnoreCase)) {
nameParts.Pop();
}
if (nameParts.Count > 0) {
return nameParts.Aggregate((left, right) => left + "." + right);
}
return String.Empty;
}
示例2: GetExpressionText
public static string GetExpressionText(LambdaExpression expression)
{
// Split apart the expression string for property/field accessors to create its name
var nameParts = new Stack<string>();
var part = expression.Body;
while (part != null)
{
if (part.NodeType == ExpressionType.Call)
{
var methodExpression = (MethodCallExpression)part;
if (!IsSingleArgumentIndexer(methodExpression))
{
break;
}
nameParts.Push(GetIndexerInvocation(methodExpression.Arguments.Single(),expression.Parameters.ToArray()));
part = methodExpression.Object;
}
else if (part.NodeType == ExpressionType.ArrayIndex)
{
var binaryExpression = (BinaryExpression)part;
nameParts.Push(GetIndexerInvocation(binaryExpression.Right, expression.Parameters.ToArray()));
part = binaryExpression.Left;
}
else if (part.NodeType == ExpressionType.MemberAccess)
{
var memberExpressionPart = (MemberExpression)part;
nameParts.Push("." + memberExpressionPart.Member.Name);
part = memberExpressionPart.Expression;
}
else if (part.NodeType == ExpressionType.Parameter)
{
// Dev10 Bug #907611
// When the expression is parameter based (m => m.Something...), we'll push an empty
// string onto the stack and stop evaluating. The extra empty string makes sure that
// we don't accidentally cut off too much of m => m.Model.
nameParts.Push(String.Empty);
part = null;
}
else
{
break;
}
}
// If it starts with "model", then strip that away
if (nameParts.Count > 0 && String.Equals(nameParts.Peek(), ".model", StringComparison.OrdinalIgnoreCase))
{
nameParts.Pop();
}
if (nameParts.Count > 0)
{
return nameParts.Aggregate((left, right) => left + right).TrimStart('.');
}
return String.Empty;
}
示例3: GetExpressionText
public static string GetExpressionText(LambdaExpression expression)
{
// Crack the expression string for property/field accessors to create its name
Stack<string> nameParts = new Stack<string>();
Expression part = expression.Body;
while (part != null) {
if (part.NodeType == ExpressionType.Call) {
MethodCallExpression methodExpression = (MethodCallExpression)part;
if (!IsSingleArgumentIndexer(methodExpression)) {
break;
}
nameParts.Push(
GetIndexerInvocation(
methodExpression.Arguments.Single(),
expression.Parameters.ToArray()
)
);
part = methodExpression.Object;
}
else if (part.NodeType == ExpressionType.ArrayIndex) {
BinaryExpression binaryExpression = (BinaryExpression)part;
nameParts.Push(
GetIndexerInvocation(
binaryExpression.Right,
expression.Parameters.ToArray()
)
);
part = binaryExpression.Left;
}
else if (part.NodeType == ExpressionType.MemberAccess) {
MemberExpression memberExpressionPart = (MemberExpression)part;
nameParts.Push("." + memberExpressionPart.Member.Name);
part = memberExpressionPart.Expression;
}
else {
break;
}
}
// If it starts with "model", then strip that away
if (nameParts.Count > 0 && String.Equals(nameParts.Peek(), ".model", StringComparison.OrdinalIgnoreCase)) {
nameParts.Pop();
}
if (nameParts.Count > 0) {
return nameParts.Aggregate((left, right) => left + right).TrimStart('.');
}
return String.Empty;
}
示例4: IsPalindrome
//First solution: Reverse string then compare with original
public bool IsPalindrome(string input)
{
string newStr = input.ToLower();
Stack<char> reversedList = new Stack<char>();
foreach (var c in newStr)
{
reversedList.Push(c);
}
string reversedStr = reversedList.Aggregate("", (current, c) => current += c);
return reversedStr.Equals(newStr);
}
示例5: DepthCheck
public DepthCheck(int maxDepth, Type type)
{
_type = type;
Stack<string> stack = new Stack<string>();
if (ThreadData.Contains(Key))
{
stack = ThreadData.GetClass<Stack<string>>(Key);
}
stack.Push(type.FullName);
if (stack.Count > maxDepth)
{
throw new MapperStackException(Constants.Errors.ErrorLazyLoop.Formatted(
stack.Aggregate((x,y)=> "{0}{1}\n\r".Formatted(x,y))
));
}
ThreadData.SetValue(Key, stack);
}
示例6: GetExpressionText
public static string GetExpressionText(LambdaExpression expression)
{
var stack = new Stack<string>();
var expression1 = expression.Body;
while (expression1 != null)
{
if (expression1.NodeType == ExpressionType.Call)
{
var methodCallExpression = (MethodCallExpression)expression1;
if (IsSingleArgumentIndexer(methodCallExpression))
{
stack.Push(GetIndexerInvocation(methodCallExpression.Arguments.Single(), expression.Parameters.ToArray()));
expression1 = methodCallExpression.Object;
}
else
break;
}
else if (expression1.NodeType == ExpressionType.ArrayIndex)
{
var binaryExpression = (BinaryExpression)expression1;
stack.Push(GetIndexerInvocation(binaryExpression.Right, expression.Parameters.ToArray()));
expression1 = binaryExpression.Left;
}
else if (expression1.NodeType == ExpressionType.MemberAccess)
{
var memberExpression = (MemberExpression)expression1;
stack.Push("." + memberExpression.Member.Name);
expression1 = memberExpression.Expression;
}
else if (expression1.NodeType == ExpressionType.Parameter)
{
stack.Push(string.Empty);
expression1 = null;
}
else
break;
}
if (stack.Count > 0 && string.Equals(stack.Peek(), ".model", StringComparison.OrdinalIgnoreCase))
stack.Pop();
if (stack.Count <= 0)
return string.Empty;
return stack.Aggregate((left, right) => left + right).TrimStart(new[] { '.' });
}
示例7: Generator
public Generator(BinaryGraph tree)
{
if (tree == null) return;
_tree = tree;
_countTreeNodes(_tree);
//BinaryNode currentNode = _tree.Root;
Stack<string> conditionsStack = new Stack<string>();
Stack<BinaryNode> traverseStack = new Stack<BinaryNode>();
traverseStack.Push(_tree.Root);
//int counter = 1;
while (traverseStack.Any())
{
BinaryNode currentNode = traverseStack.Pop();
_addChildrenAvoidEndpoinDoubleAdding(ref traverseStack, currentNode);
if (currentNode.Type == NodeType.Expression)
{
//String.Join(" & ", conditionsStack);
char[] trimChars = { ' ', '&' }; //TODO: use Smv.OrTrimChars & Smv.AndTrimChars
//_outputLines.Add(new OutputLine(currentNode.Counter, currentNode.));
string[] varval = currentNode.Value.Split(new string[] { ":=" }, StringSplitOptions.RemoveEmptyEntries);
string condition = conditionsStack.Aggregate("", (current, s) => current + ("(" + s + ") & ")).TrimEnd(trimChars);
if (conditionsStack.Count > 1)
{
condition = String.Format("({0})", condition);
}
_outputLines.Add(new OutputLine(currentNode.Counter, varval[0].Trim(), condition, varval[1].Trim()));
if (_conditionEndPointLeftBranchChild(currentNode))
{
string lastCondition = conditionsStack.Pop();
conditionsStack.Push(String.Format("!({0})", lastCondition));
}
}
else if (currentNode.Type == NodeType.Condition)
{
conditionsStack.Push(currentNode.Value);
}
else if (currentNode.Type == NodeType.ConditionEndPoint)
{
conditionsStack.Pop();
}
else throw new Exception("Unsupported node type throw instruction enumeration");
}
//_outputLines.Sort((a, b) => String.CompareOrdinal(a.Variable, b.Variable)); //Sort by variable name
}
示例8: GetExpressionText
public static string GetExpressionText(LambdaExpression expression)
{
if (expression == null)
{
throw new ArgumentNullException(nameof(expression));
}
// Split apart the expression string for property/field accessors to create its name
var nameParts = new Stack<string>();
var part = expression.Body;
while (part != null)
{
if (part.NodeType == ExpressionType.Call)
{
var methodExpression = (MethodCallExpression)part;
if (!IsSingleArgumentIndexer(methodExpression))
{
// Unsupported.
break;
}
nameParts.Push(
GetIndexerInvocation(
methodExpression.Arguments.Single(),
expression.Parameters.ToArray()));
part = methodExpression.Object;
}
else if (part.NodeType == ExpressionType.ArrayIndex)
{
var binaryExpression = (BinaryExpression)part;
nameParts.Push(
GetIndexerInvocation(
binaryExpression.Right,
expression.Parameters.ToArray()));
part = binaryExpression.Left;
}
else if (part.NodeType == ExpressionType.MemberAccess)
{
var memberExpressionPart = (MemberExpression)part;
var name = memberExpressionPart.Member.Name;
// If identifier contains "__", it is "reserved for use by the implementation" and likely compiler-
// or Razor-generated e.g. the name of a field in a delegate's generated class.
if (name.Contains("__"))
{
// Exit loop. Should have the entire name because previous MemberAccess has same name as the
// leftmost expression node (a variable).
break;
}
nameParts.Push("." + name);
part = memberExpressionPart.Expression;
}
else if (part.NodeType == ExpressionType.Parameter)
{
// When the expression is parameter based (m => m.Something...), we'll push an empty
// string onto the stack and stop evaluating. The extra empty string makes sure that
// we don't accidentally cut off too much of m => m.Model.
nameParts.Push(string.Empty);
// Exit loop. Have the entire name because the parameter cannot be used as an indexer; always the
// leftmost expression node.
break;
}
else
{
// Unsupported.
break;
}
}
// If parts start with "model", then strip that part away.
if (nameParts.Count > 0 && string.Equals(nameParts.Peek(), ".model", StringComparison.OrdinalIgnoreCase))
{
nameParts.Pop();
}
if (nameParts.Count > 0)
{
return nameParts.Aggregate((left, right) => left + right).TrimStart('.');
}
return string.Empty;
}
示例9: BuildResourceKey
internal static string BuildResourceKey(Type cotnainerType, Stack<string> keyStack)
{
return BuildResourceKey(cotnainerType, keyStack.Aggregate(string.Empty, (prefix, name) => BuildResourceKey(prefix, name)));
}
示例10: TryGetPropertyInvocationChain
private bool TryGetPropertyInvocationChain( IExpression expression, out string invocationPath )
{
invocationPath = null;
Stack<string> invocationStack = new Stack<string>();
IExpression currentExpression = expression;
while ((currentExpression is IMethodCallExpression && currentExpression.SyntaxElementKind != SyntaxElementKind.This) ||
currentExpression.SyntaxElementKind == SyntaxElementKind.Box)
{
if (currentExpression.SyntaxElementKind == SyntaxElementKind.Box)
{
currentExpression = (currentExpression as IUnaryExpression).Value;
}
else
{
IMethodCallExpression methodCallExpression = (IMethodCallExpression)currentExpression;
if (!(methodCallExpression.Method.IsSpecialName && methodCallExpression.Method.Name.StartsWith("get_")))
{
return false;
}
invocationStack.Push(((IMethodCallExpression)currentExpression).Method.Name.Substring(4));
currentExpression = methodCallExpression.Instance;
}
}
if ( currentExpression == null ||
(currentExpression.SyntaxElementKind != SyntaxElementKind.This && currentExpression.SyntaxElementKind != SyntaxElementKind.Field) )
{
return false;
}
IFieldExpression fieldExpression = currentExpression as IFieldExpression;
if ( fieldExpression != null )
{
invocationStack.Push( fieldExpression.Field.Name );
}
invocationPath = invocationStack.Aggregate( new StringBuilder(), ( builder, s ) => builder.Append( '.' ).Append( s ) ).Remove( 0, 1 ).ToString();
return true;
}
示例11: CheckMethod
private void CheckMethod (MethodDefinition method, Stack<string> stack)
{
if (!method.HasBody)
return;
// avoid looping if we're sure there's no call in the method
if (!OpCodeBitmask.Calls.Intersect (OpCodeEngine.GetBitmask (method)))
return;
string method_name = method.ToString ();
// check to avoid constructors calling recursive methods
if (stack.Contains (method_name))
return;
// check constructor for virtual method calls
foreach (Instruction current in method.Body.Instructions) {
switch (current.OpCode.Code) {
case Code.Call:
case Code.Callvirt:
// we recurse into normal calls since they might be calling virtual methods
MethodDefinition md = (current.Operand as MethodDefinition);
if (md == null || md.IsConstructor || !md.HasThis)
continue;
// check that the method is it this class or a subclass
if (!IsSubsclass (md.DeclaringType, method.DeclaringType))
continue;
// check that we're not calling the method on another object
if (!IsCallFromInstance (current.Previous, md.Parameters.Count))
continue;
if (md.IsVirtual && !md.IsFinal) {
string s = stack.Count == 0 ? method_name : stack.Aggregate ((a1, a2) => a1 + ", " + Environment.NewLine + a2);
s = String.Format ("Calling a virtual method, '{0}' from {1}.", md, s);
Runner.Report (method, current, Severity.High, Confidence.High, s);
} else {
stack.Push (method_name);
CheckMethod (md, stack);
stack.Pop ();
}
break;
}
}
}