本文整理汇总了C#中System.Linq.Expressions.NewExpression类的典型用法代码示例。如果您正苦于以下问题:C# NewExpression类的具体用法?C# NewExpression怎么用?C# NewExpression使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
NewExpression类属于System.Linq.Expressions命名空间,在下文中一共展示了NewExpression类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: VisitNew
protected override Expression VisitNew(NewExpression node)
{
var originalConstructorParameters = node.Arguments.Select(x => x.Type).ToList();
var genericType = node.Constructor.DeclaringType.GetGenericTypeDefinition();
var genericTypeParameters = genericType.GetTypeInfo().GenericTypeArguments.ToList();
var originalGenericArgs = node.Constructor.DeclaringType.GetTypeInfo().GenericTypeArguments.ToList();
if (genericTypeParameters.Count != _newGenericArgs.Length)
{
throw new InvalidOperationException("Wrong number of generic argument values specified. This type requires " + genericTypeParameters.Count + " generic type arguments");
}
for (int i = 0; i < _newGenericArgs.Length; i++)
{
var constraints = genericTypeParameters[i].GetTypeInfo().GetGenericParameterConstraints().ToList();
foreach (var constraint in constraints)
{
if (!constraint.GetTypeInfo().IsAssignableFrom(_newGenericArgs[i].GetTypeInfo()))
{
throw new InvalidOperationException("Generic type parameter " + i + " is constrained to type " + constraint + ". The supplied type " + _newGenericArgs[i] + " does not meet this constraint");
}
}
}
result = genericType.MakeGenericType(_newGenericArgs.ToArray()).GetTypeInfo().DeclaredConstructors.Where(x => x == node.Constructor).Single();
throw new VisitStoppedException();
}
示例2: UpdateListInit
protected ListInitExpression UpdateListInit(ListInitExpression init, NewExpression nex, IEnumerable<ElementInit> initializers)
{
if (nex != init.NewExpression || initializers != init.Initializers) {
return Expression.ListInit(nex, initializers);
}
return init;
}
示例3: VisitNew
/// <summary>
/// Visits new object expression like new DateTime().
/// </summary>
/// <param name="node">Node to visit.</param>
/// <returns>Original node.</returns>
protected override Expression VisitNew(NewExpression node)
{
var newObject = node.Constructor.Invoke(node.Arguments.Select(a => ((ConstantExpression)a).Value).ToArray());
PrintConstant(newObject);
return node;
}
示例4: VisitNew
protected override Expression VisitNew(NewExpression newExpression)
{
Check.NotNull(newExpression, nameof(newExpression));
var newNewExpression = base.VisitNew(newExpression);
var selectExpression = QueryModelVisitor.TryGetQuery(_querySource);
if (selectExpression != null)
{
for (var i = 0; i < newExpression.Arguments.Count; i++)
{
var aliasExpression
= selectExpression.Projection
.OfType<AliasExpression>()
.SingleOrDefault(ae => ae.SourceExpression == newExpression.Arguments[i]);
if (aliasExpression != null)
{
aliasExpression.SourceMember
= newExpression.Members?[i]
?? (newExpression.Arguments[i] as MemberExpression)?.Member;
}
}
}
return newNewExpression;
}
示例5: SerializableNewExpression
/// <summary>
/// Constructor with an <see cref="NewExpression"/> and an <see cref="ExpressionConverter"/>.
/// </summary>
/// <param name="expression">The original, not serializable <see cref="Expression"/>.</param>
/// <param name="expConverter">The <see cref="ExpressionConverter"/> to convert contained <see cref="Expression">Expressions</see>.</param>
public SerializableNewExpression(NewExpression expression, ExpressionConverter expConverter)
: base(expression, expConverter)
{
Arguments = expression.Arguments.MakeSerializableCollection<SerializableExpression>(expConverter);
Members = InterLinqTypeSystem.Instance.GetCollectionOf<InterLinqMemberInfo>(expression.Members);
Constructor = InterLinqTypeSystem.Instance.GetInterLinqVersionOf<InterLinqConstructorInfo>(expression.Constructor);
}
示例6: VisitNew
protected override Expression VisitNew(NewExpression node)
{
// specialize for DateTime
if (FormatIfExprIsDateTime(node)) return node;
// promising type initializer(goto:VisitMemberInit)
if (!node.Type.IsAnonymousType()) return node;
var indent = BuildIndent();
var innerTranslator = new BigQueryTranslateVisitor(depth, indentSize);
var merge = node.Members.Zip(node.Arguments, (x, y) =>
{
var rightValue = innerTranslator.VisitAndClearBuffer(y);
if (x.Name == rightValue.Trim('[', ']')) return "[" + x.Name + "]";
return rightValue + " AS " + "[" + x.Name + "]";
});
var command = string.Join("," + Environment.NewLine,
merge.Select(x => indent + x));
sb.Append(command);
return node;
}
示例7: LetSelectExpressionNode
public LetSelectExpressionNode(MethodCallExpressionParseInfo parseInfo, LambdaExpression selector)
: base(Utils.CheckNotNull("parseInfo", parseInfo), Utils.CheckNotNull("selector", selector))
{
// Heuristically detect "let" constructs
// Assume a Select call is a "let" construct if its selector looks like this: <x> => new { <x> = <x>, <y> = ... }
var selectorBody = selector.Body as NewExpression;
if (selectorBody != null
&& selectorBody.Arguments.Count == 2
&& selectorBody.Arguments[0] == selector.Parameters[0]
//&& selectorBody.Constructor.DeclaringType.IsDefined(typeof(CompilerGeneratedAttribute), false)
&& selectorBody.Members != null
&& selectorBody.Members.Count == 2)
{
_letConstruction = selectorBody;
_resolvedAdaptedSelector = new ResolvedExpressionCache<Expression>(this);
_resolvedLetExpression = new ResolvedExpressionCache<Expression>(this);
}
else
{
_letConstruction = null;
_resolvedAdaptedSelector = null;
_resolvedLetExpression = null;
}
}
示例8: Main
static void Main(string[] args)
{
//int maxCount = 40000;
//List<HotSpotEntities> collection = new List<HotSpotEntities>();
//for (int i = 0; i < maxCount; i++)
//{
// Console.WriteLine(string.Format("成功创建连接对象{0}", i));
// var db = new HotSpotEntities();
// db.Connection.Open();
// collection.Add(db);
//}
Expression<Func<int, bool>> exp1 = x => x > 5;
Expression<Func<int, bool>> exp2 = x => x < 10;
ParameterExpression y = Expression.Parameter(typeof(int), "y");
var newExp = new NewExpression(y);
var newexp1 = newExp.Replace(exp1.Body);
var newexp2 = newExp.Replace(exp2.Body);
var newbody = Expression.And(newexp1, newexp2);
Expression<Func<int, bool>> res = Expression.Lambda<Func<int, bool>>(newbody, y);
//将表达式树描述的lambda表达式编译为可执行代码,并生成表示该lambda表达式的委托
Func<int, bool> del = res.Compile();
Console.WriteLine(del(7));
Console.ReadLine();
}
示例9: Update
public ListInitExpression Update(NewExpression newExpression, IEnumerable<ElementInit> initializers)
{
Contract.Requires(newExpression != null);
Contract.Requires(initializers != null);
Contract.Ensures(Contract.Result<ListInitExpression>() != null);
return default(ListInitExpression);
}
示例10: UpdateMemberInit
protected MemberInitExpression UpdateMemberInit(MemberInitExpression init, NewExpression nex, IEnumerable<MemberBinding> bindings)
{
if (nex != init.NewExpression || bindings != init.Bindings) {
return Expression.MemberInit(nex, bindings);
}
return init;
}
示例11: Update
public MemberInitExpression Update(NewExpression newExpression, IEnumerable<MemberBinding> bindings)
{
Contract.Requires(newExpression != null);
Contract.Requires(bindings != null);
Contract.Ensures(Contract.Result<MemberInitExpression>() != null);
return default(MemberInitExpression);
}
示例12: InvokeConstructor
private object InvokeConstructor(NewExpression expression, object[] args, out int argumentCount)
{
object valueToSet;
int nestedArgumentCount;
argumentCount = 0;
ArrayList argList = new ArrayList();
int i = 0;
foreach (var arg in expression.Arguments)
{
switch (arg.NodeType)
{
case ExpressionType.New:
valueToSet = InvokeConstructor((NewExpression)arg, args.Skip(i).ToArray(), out nestedArgumentCount);
i += nestedArgumentCount;
break;
case ExpressionType.MemberInit:
valueToSet = InvokeMemberInitExpression((MemberInitExpression)arg,
args.Skip(i).ToArray(), out nestedArgumentCount);
i += nestedArgumentCount;
break;
default:
valueToSet = LinqUtil.ChangeType(args[i], arg.Type);
i++;
break;
}
argList.Add(valueToSet);
}
argumentCount = i;
return expression.Constructor.Invoke(argList.ToArray());
}
示例13: VisitNew
protected override Expression VisitNew(NewExpression node)
{
var newNode = (NewExpression)base.VisitNew(node);
if (newNode.Type.IsGenericType
&& newNode.Type.GetGenericTypeDefinition() == typeof(HashSet<>)
&& newNode.Arguments.Count == 1
&& newNode.Arguments[0] is AccumulatorExpression
&& ((AccumulatorExpression)newNode.Arguments[0]).AccumulatorType == AccumulatorType.Push)
{
Guid correlationId = Guid.Empty;
if (_groupMap == null || TryGetCorrelatedGroup(node.Arguments[0], out correlationId))
{
Expression accumulator = new AccumulatorExpression(
newNode.Type,
AccumulatorType.AddToSet,
((AccumulatorExpression)newNode.Arguments[0]).Argument);
if (_groupMap != null)
{
accumulator = new CorrelatedAccumulatorExpression(
correlationId,
(AccumulatorExpression)accumulator);
}
return accumulator;
}
}
return newNode;
}
示例14: VisitNew
protected override Expression VisitNew(NewExpression nex)
{
nex = (NewExpression)base.VisitNew(nex);
var instance = (Expression)null;
var method = (MethodBase)null;
return Matcher
.For(nex)
.Type(t => t.IsSubclassOf<Delegate>())
.Argument(0).AssignTo(out instance)
.For(nex)
.Argument(1).As<AddressOfExpression>()
.Do(x => method = x.Method)
.IfMatched<Expression>(
() => Expression.Call(null, CreateDelegateMethodInfo, new[] {
Expression.Constant(nex.Type),
instance,
Expression.Constant(method),
Expression.Constant(true)
}).Convert(nex.Type),
nex
);
}
示例15: VisitNew
protected override Expression VisitNew(NewExpression node)
{
var args = node.Arguments.Cast<ConstantExpression>().Select(c => c.Value).ToArray();
var value = node.Constructor.Invoke(args);
var cex = Expression.Constant(value);
return Visit(cex);
}