本文整理汇总了C#中Expression.GetType方法的典型用法代码示例。如果您正苦于以下问题:C# Expression.GetType方法的具体用法?C# Expression.GetType怎么用?C# Expression.GetType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Expression
的用法示例。
在下文中一共展示了Expression.GetType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: OccurrenceOf
public int OccurrenceOf(Expression exp) {
if (!IsLeaf())
return LChild.OccurrenceOf(exp) + (RChild?.OccurrenceOf(exp) ?? 0);
if (exp.GetType() == Data.GetType()) {
Console.WriteLine(exp.GetType());
Console.WriteLine(Data.GetType());
if (Data is NameSegment) {
var ns1 = (NameSegment)Data;
var ns2 = (NameSegment)exp;
if (ns1.Name == ns2.Name)
return 1;
} else if (Data is LiteralExpr) {
var ns1 = (LiteralExpr)Data;
var ns2 = (LiteralExpr)exp;
if (ns1.Value == ns2.Value)
return 1;
} else if (Data is UnaryOpExpr) {
var ns1 = (UnaryOpExpr)Data;
var ns2 = (UnaryOpExpr)exp;
var e1 = (NameSegment)ns1.E;
var e2 = (NameSegment)ns2.E;
if (e1.Name == e2.Name)
return 1;
}
}
return 0;
}
示例2: GetFormatter
public static IExpressionFormatter GetFormatter( IIndentable indentable, Expression expression )
{
Type formatterType;
if ( !_formatters.TryGetValue( expression.GetType(), out formatterType ) )
return new DefaultExpressionFormatter( expression );
var formatter = Activator.CreateInstance( formatterType, expression ) as IExpressionFormatter;
( formatter as IIndentable ).IndentLevel = indentable.IndentLevel;
( formatter as IIndentable ).Indent = indentable.Indent;
if ( formatter == null )
throw new ArgumentNullException( "Formatter not instantiated: " + formatterType.Name );
return formatter;
}
示例3: ReplaceTerm
private void ReplaceTerm(Expression old_singleton, Expression new_term, ExpressionTree formula, ref List<Expression> nexp)
{
Contract.Requires(nexp != null);
Contract.Requires(old_singleton != null);
Contract.Requires(new_term != null);
NameSegment curNs = null;
NameSegment oldNs = null;
if (formula == null)
return;
if (formula.isLeaf())
{
if (formula.data.GetType() == old_singleton.GetType() && formula.was_replaced == false)
{
if (formula.data is NameSegment)
{
curNs = (NameSegment)formula.data;
oldNs = (NameSegment)old_singleton;
}
else if (formula.data is UnaryOpExpr)
{
curNs = (NameSegment)((UnaryOpExpr)formula.data).E;
oldNs = (NameSegment)((UnaryOpExpr)old_singleton).E;
}
else
Contract.Assert(false, Util.Error.MkErr(formula.data, -1));
if (curNs.Name == oldNs.Name)
{
ExpressionTree nt = formula.Copy();
nt.data = new_term;
if (nt.parent.lChild == nt)
nt.parent.lChild = nt;
else
nt.parent.rChild = nt;
nexp.Add(nt.root.TreeToExpression());
}
}
return;
}
ReplaceTerm(old_singleton, new_term, formula.lChild, ref nexp);
ReplaceTerm(old_singleton, new_term, formula.rChild, ref nexp);
}
示例4: MakeYield
public static YieldExpression MakeYield(LabelTarget target, Expression value, int yieldMarker)
{
ContractUtils.RequiresNotNull(target, "target");
ContractUtils.Requires(target.Type != typeof(void), "target", "generator label must have a non-void type");
if (value != null) {
if (!TypeUtils.AreReferenceAssignable(target.Type, value.Type)) {
// C# autoquotes generator return values
if (target.Type.IsSubclassOf(typeof(Expression)) &&
TypeUtils.AreAssignable(target.Type, value.GetType())) {
value = Expression.Quote(value);
}
throw new ArgumentException(string.Format("Expression of type '{0}' cannot be yielded to a generator label of type '{1}'", value.Type, target.Type));
}
}
return new YieldExpression(target, value, yieldMarker);
}
示例5: UsualArithmeticConversions
private static Pair<Expression, Expression> UsualArithmeticConversions(
Expression lop, Expression rop)
{
Symbols.Type lt = lop.GetType(), rt = rop.GetType();
if (lt is Symbols.DOUBLE || rt is Symbols.DOUBLE)
{
if (rt != lt)
{
if (lt is Symbols.DOUBLE) { rop = new Cast(rop, lt); }
else { lop = new Cast(lop, rt); }
}
}
else if (lt is Symbols.CHAR && rt is Symbols.INT) { lop = new Cast(lop, rt); }
else if (rt is Symbols.CHAR && lt is Symbols.INT) { rop = new Cast(rop, lt); }
return new Pair<Expression, Expression>(lop, rop);
}
示例6: TestIdenticality
/// <summary>
/// Tests a pair of expressions for identicality using the equality definitions defined in this class.
/// </summary>
/// <param name="E1">The first expression to test</param>
/// <param name="E2">The second expression to test</param>
public static bool TestIdenticality(Expression E1, Expression E2)
{
//Test for "null" arguments
if ((E1 as object) == null && (E2 as object) == null) return true;
if ((E1 as object) == null || (E2 as object) == null) return false;
//To be identical, they must have the same type
if (E1.GetType() != E2.GetType()) return false;
//If E1 maps to E2
if (E1.MapsTo(E2)) return true;
//Fast tests amoungst operands
if ((E1 is Irreducible) && (E2 is Irreducible)) return TestIdenticality_FastTests_Operands(E1, E2);
//Test for identicality amoungst binary operations
if (((E1 is Operation) && (E1 as Operation).Arity == 2) && ((E2 is Operation) && (E2 as Operation).Arity == 2)) return TestEquality(E1, E2, BinaryOperations);
//If we couldn't determine that they are equal:
return false;
}
示例7: Contains
public static bool Contains(Expression expr, Expression sub)
{
if (expr.GetType().Equals(sub.GetType()))
return expr.Equals(sub);
if (expr is BinaryExpression)
{
BinaryExpression bexpr = (BinaryExpression)expr;
return Contains(bexpr.Left, sub) && Contains(bexpr.Right, sub);
}
else if (expr is LinearExpression)
{
LinearExpression lexpr = (LinearExpression)expr;
bool contains = true;
foreach (Expression item in lexpr.List)
contains = contains && Contains(item, sub);
return contains;
}
else if (expr is FunctionExpression)
{
FunctionExpression fexpr = (FunctionExpression)expr;
bool contains = true;
foreach (Expression arg in fexpr.Arguments)
contains = contains && Contains(arg, sub);
return contains;
}
return false;
}
示例8: Quote
///<summary>Creates a <see cref="T:System.Linq.Expressions.UnaryExpression" /> that represents an expression that has a constant value of type <see cref="T:System.Linq.Expressions.Expression" />.</summary>
///<returns>A <see cref="T:System.Linq.Expressions.UnaryExpression" /> that has the <see cref="P:System.Linq.Expressions.Expression.NodeType" /> property equal to <see cref="F:System.Linq.Expressions.ExpressionType.Quote" /> and the <see cref="P:System.Linq.Expressions.UnaryExpression.Operand" /> property set to the specified value.</returns>
///<param name="expression">An <see cref="T:System.Linq.Expressions.Expression" /> to set the <see cref="P:System.Linq.Expressions.UnaryExpression.Operand" /> property equal to.</param>
///<exception cref="T:System.ArgumentNullException">
///<paramref name="expression" /> is null.</exception>
public static UnaryExpression Quote(Expression expression) {
RequiresCanRead(expression, "expression");
bool validQuote = expression is LambdaExpression;
#if SILVERLIGHT
validQuote |= SilverlightQuirks;
#endif
if (!validQuote) throw Error.QuotedExpressionMustBeLambda();
return new UnaryExpression(ExpressionType.Quote, expression, expression.GetType(), null);
}
示例9: Of
public static int Of(Expression op)
{
return Of(op.GetType());
}
示例10: GenerateExpression
private void GenerateExpression(Expression expr, Type expectedType)
{
Type deliveredType;
if (expr is StringLiteral)
{
deliveredType = typeof(string);
ilgenerator.Emit(OpCodes.Ldstr, ((StringLiteral)expr).Value);
}
else if (expr is IntegerLiteral)
{
deliveredType = typeof(int);
ilgenerator.Emit(OpCodes.Ldc_I4, ((IntegerLiteral)expr).Value);
}
else if (expr is Variable)
{
var ident = ((Variable)expr).Ident;
deliveredType = TypeOfExpr(expr);
if (!symbolsTable.ContainsKey(ident))
{
throw new CodeGeneratorException("Undeclared variable '" + ident + "'");
}
ilgenerator.Emit(OpCodes.Ldloc, symbolsTable[ident]);
}
else if (expr is BinaryExpression)
{
deliveredType = typeof(int);
GenerateExpression(((BinaryExpression)expr).Left, typeof(int));
GenerateExpression(((BinaryExpression)expr).Right, typeof(int));
var binExpr = (BinaryExpression)expr;
switch (binExpr.Op)
{
case BinaryOperator.Add:
ilgenerator.Emit(OpCodes.Add);
break;
case BinaryOperator.Sub:
ilgenerator.Emit(OpCodes.Sub);
break;
case BinaryOperator.Mul:
ilgenerator.Emit(OpCodes.Mul);
break;
case BinaryOperator.Div:
ilgenerator.Emit(OpCodes.Div);
break;
}
}
else
{
throw new CodeGeneratorException("Unable to generate " + expr.GetType().Name);
}
if (deliveredType != expectedType)
{
if (deliveredType == typeof(int) && expectedType == typeof(string))
{
ilgenerator.Emit(OpCodes.Box, typeof(int));
ilgenerator.Emit(OpCodes.Callvirt, typeof(object).GetMethod("ToString"));
}
else
{
throw new CodeGeneratorException("Can't convert type " + deliveredType.Name + " to " + expectedType.Name);
}
}
}
示例11: TypeOfExpr
private Type TypeOfExpr(Expression expr)
{
if (expr is StringLiteral)
{
return typeof(string);
}
if (expr is IntegerLiteral)
{
return typeof(int);
}
if (expr is Variable)
{
var var = (Variable)expr;
if (symbolsTable.ContainsKey(var.Ident))
{
return symbolsTable[var.Ident].LocalType;
}
throw new CodeGeneratorException("Undeclared variable '" + var.Ident + "'");
}
if (expr is BinaryExpression)
{
return typeof(int);
}
throw new CodeGeneratorException("Unable to find the type of " + expr.GetType().Name);
}
示例12: Solve
static Expression Solve(Expression left, Expression right, Variable v)
{
left = left.Simplify();
right = right.Simplify();
Console.WriteLine("\t{0} = {1} // {2} == {3}", left, right, left.GetType().Name, right.GetType().Name);
if (!left.IsDependOf(v)) throw new Exception("Can't solve equation. No dependencies of variable '" + v.Name + "'.");
if (left is Symbolic.Operators.Scalar.Add)
{
Expression ll = Left(left);
Expression lr = Right(left);
if (lr.IsDependOf(v)) return Solve(lr, right - ll, v);
else return Solve(ll, right - lr, v);
}
else if (left is Symbolic.Operators.Scalar.Sub)
{
Expression ll = Left(left);
Expression lr = Right(left);
if (lr.IsDependOf(v)) return Solve(-lr, right - ll, v);
else return Solve(ll, right + lr, v);
}
else if (left is Symbolic.Operators.Scalar.Div)
{
Expression ll = Left(left);
Expression lr = Right(left);
if (lr.IsDependOf(v)) return Solve(lr, ll / right, v);
else return Solve(ll, right * lr, v);
}
else if (left is Symbolic.Operators.Scalar.Mul)
{
Expression ll = Left(left);
Expression lr = Right(left);
if (lr.IsDependOf(v)) return Solve(lr, right / ll, v);
else return Solve(ll, right / lr, v);
}
else if (left is Symbolic.Operators.Scalar.Pow)
{
Expression ll = Left(left);
Expression lr = Right(left);
if (lr.IsDependOf(v)) return Solve(lr, SMath.Log(right, ll), v);
else return Solve(ll, right ^ (1 / lr), v);
}
else if (left is Symbolic.Functions.Sqrt)
{
Expression ll = (left as Symbolic.Functions.Sqrt).Args[0];
return Solve(ll, right ^ 2, v);
}
else if (left is Symbolic.Functions.Sin)
{
Expression ll = (left as Symbolic.Functions.Sin).Args[0];
return Solve(ll, SMath.ArcSin(right), v);
}
else if (left is Symbolic.Functions.Cos)
{
Expression ll = (left as Symbolic.Functions.Cos).Args[0];
return Solve(ll, SMath.ArcCos(right), v);
}
else if (left is Symbolic.Functions.Tan)
{
Expression ll = (left as Symbolic.Functions.Tan).Args[0];
return Solve(ll, SMath.ArcTan(right), v);
}
else if (left is Symbolic.Functions.ArcSin)
{
Expression ll = (left as Symbolic.Functions.ArcSin).Args[0];
return Solve(ll, SMath.Sin(right), v);
}
else if (left is Symbolic.Functions.ArcCos)
{
Expression ll = (left as Symbolic.Functions.ArcCos).Args[0];
return Solve(ll, SMath.Cos(right), v);
}
else if (left is Symbolic.Functions.ArcTan)
{
Expression ll = (left as Symbolic.Functions.ArcTan).Args[0];
return Solve(ll, SMath.Tan(right), v);
}
else if (left is Symbolic.Functions.Exp)
{
Expression ll = (left as Symbolic.Functions.Exp).Args[0];
return Solve(ll, SMath.Ln(right), v);
}
else if (left is Symbolic.Functions.Ln)
{
Expression ll = (left as Symbolic.Functions.Ln).Args[0];
return Solve(ll, SMath.Exp(right), v);
}
else if (left is Variable && left.IsDependOf(v)) return right;
else throw new Exception("Can't solve equation. Unknown equation type.");
}
示例13: PostfixConvertResult
public static BinaryConvertResult PostfixConvertResult(Expression lop, Expression rop,
PostfixOperator op)
{
BinaryConvertResult res = new BinaryConvertResult();
switch (op)
{
case PostfixOperator.REF:
case PostfixOperator.DOT:
Symbols.Type t = lop.GetType() is Symbols.POINTER && op == PostfixOperator.REF?
((Symbols.POINTER)lop.GetType()).GetRefType() : lop.GetType();
if (!(t is Symbols.RECORD))
{
throw new Symbols.Exception(lop, NOT_STRUCT);
}
if (!((Symbols.RECORD)t).GetTable().ContainsVariable(((Identifier)rop).GetName()))
{
throw new Symbols.Exception(rop,
string.Format(NOT_MEMBER, ((Identifier)rop).GetName(), t.GetName())
);
}
res.lvalue = true;
res.type = (((Symbols.RECORD)t).GetTable().GetVariable(((Identifier)rop).GetName())).GetType();
break;
case PostfixOperator.INDEX:
if (!(lop.GetType() is Symbols.POINTER))
{
throw new Symbols.Exception(lop, NOT_POINTER);
}
if (!rop.GetType().IsInteger())
{
throw new Symbols.Exception(rop, NOT_INTEGER);
}
res.lvalue = true;
res.type = ((Symbols.RefType)lop.GetType()).GetRefType();
break;
}
res.left = lop;
res.right = rop;
return res;
}
示例14: InvalidExpressionException
public InvalidExpressionException(string message, Expression expr, params Type[] options)
: base(message +
(expr == null ? " received null expression " : " received expression of type " + expr.GetType().Name) +
(options == null ? "" : " Valid options include: " + string.Join(",", options.Select(o=>o.Name))))
{
}
示例15: RecordExpression
/// <summary>
/// Records information about the given expression, under the given node.
/// </summary>
/// <param name="expression">
/// The expression to record.
/// </param>
/// <param name="parentNode">
/// The Xml node to record this expression beneath.
/// </param>
/// <returns>
/// Returns the new Xml node describing this expression.
/// </returns>
private static XmlNode RecordExpression(Expression expression, XmlNode parentNode)
{
Param.AssertNotNull(expression, "expression");
Param.AssertNotNull(parentNode, "parentNode");
// Create a new node for this expression and add it to the parent.
XmlNode expressionNode = parentNode.OwnerDocument.CreateElement("Expression");
parentNode.AppendChild(expressionNode);
// Add the name and contents of the expression.
if (expression.ExpressionType == ExpressionType.Literal)
{
XmlAttribute text = parentNode.OwnerDocument.CreateAttribute("Text");
text.Value = expression.ToString();
expressionNode.Attributes.Append(text);
}
XmlAttribute type = parentNode.OwnerDocument.CreateAttribute("Type");
type.Value = expression.GetType().Name;
expressionNode.Attributes.Append(type);
return expressionNode;
}