Netty Camel样品(Netty Camel samples)
我是Netty的新手。
我正在寻找一些样品。 (优选但不是必须使用Camel Netty Component和Spring)
特别是一个使用TCP消息的示例Netty应用程序。
另外,我如何编写可以测试这个netty应用程序的JUnit测试?
谢谢,Dar
I'm a newbie to Netty.
I'm looking for some samples. (Preferably but not necessarity using Camel Netty Component and Spring)
Specifically a sample Netty app that consumes TCP messages.
Also how can I write a JUnit test that can test this netty app?
Thanks, Dar
原文:https://stackoverflow.com/questions/8419245
最满意答案
这里有足够的例子来使用表达式来获取一个属性或字段的名称来启动它:
public static MemberInfo GetMemberInfo<T, U>(Expression<Func<T, U>> expression) { var member = expression.Body as MemberExpression; if (member != null) return member.Member; throw new ArgumentException("Expression is not a member access", "expression"); }
调用代码如下所示:
public class Program { public string Name { get { return "My Program"; } } static void Main() { MemberInfo member = ReflectionUtility.GetMemberInfo((Program p) => p.Name); Console.WriteLine(member.Name); } }
尽管如此:
(Program p) => p.Name
的简单(Program p) => p.Name
实际上涉及相当多的工作(并且可能花费大量时间)。 考虑缓存结果,而不是经常调用该方法。Here's enough of an example of using Expressions to get the name of a property or field to get you started:
public static MemberInfo GetMemberInfo<T, U>(Expression<Func<T, U>> expression) { var member = expression.Body as MemberExpression; if (member != null) return member.Member; throw new ArgumentException("Expression is not a member access", "expression"); }
Calling code would look like this:
public class Program { public string Name { get { return "My Program"; } } static void Main() { MemberInfo member = ReflectionUtility.GetMemberInfo((Program p) => p.Name); Console.WriteLine(member.Name); } }
A word of caution, though: the simple statment of
(Program p) => p.Name
actually involves quite a bit of work (and can take measurable amounts of time). Consider caching the result rather than calling the method frequently.
相关问答
更多-
这里有足够的例子来使用表达式来获取一个属性或字段的名称来启动它: public static MemberInfo GetMemberInfo
(Expression > expression) { var member = expression.Body as MemberExpression; if (member != null) return member.Member; throw new ArgumentException( ... -
动态lambda表达式(OrderBy)和可空属性类型(Dynamic lambda expression (OrderBy) and nullable property type)[2022-06-13]
没有理由使用IComparable 。 实际上,许多类似的类型都没有实现IComparable 。 只需使用您传递的任何运行时类型: var finalExpression = Expression.Call( typeof (Queryable), "OrderBy", new[] { list.ElementType, propertyToOrder.Type }, list.Expression, Expression.Lambda(propertyToOrde ... -
lambda表达式作为属性(lambda Expression as a property)[2022-09-01]
你可以尝试这样的事情,不必将属性名称编码为字符串,如果这是你所说的强类型的意思: class CellInfo{ public string Title { get; set; } public string FormatString { get; set; } public Func Selector { get; set; } } Dictionary > dict = new Dictionary< ... -
从属性名称获取属性lambda(其中属性类型可以为空)(get property lambda from property Name (Where property type can be nullable))[2022-11-20]
其实我不认为这个问题与Nullable类型有关,而是与值类型有关。 用一个decimal类型的属性(而不是Nullable)来尝试你的方法:它会以同样的方式失败。 看看如何为值和引用类型生成表达式树(例如使用LinqPad) Expression > lambda = x => x.AString; (参考类型) =>正文是MemberExpression Expression > lambda = x => x.ADecima ... -
我可能会误解,但直接x => x将是一个ParameterExpression 。 只需在现有is MemberExpression测试下添加一个额外的测试: if (expression is MemberExpression) { // As-is } // New condition if (expression is ParameterExpression) { return expression.Type.Name; } 使用此代码: class Car { public str ...
-
您可以在表达式上使用反射 foreach (var p in stage.GetType().GetProperties()) { } 但是,如果你这样做可能会有相当大的性能损失,而且它可以打开一整套安全蠕虫,特别是如果你使用你的类来访问数据库。 但是,您可以扩展这个想法,并定义一些属性来标记隐藏的字段,并使用您的扩展方法来挑选它们。 You can use reflection on expression foreach (var p i ...
-
你有什么理由不想将lambda表达式直接放在GroupBy调用中吗? 这就是它通常挂在一起的方式: var groupedData = myDataCollection.GroupBy(md => new { md.Property1, md.Property2, md.Property ...
-
F#和C#使用不同的策略为委托发出Invoke方法。 如果您尝试在C#中定义类似的委托 - 委托类型将是内部但具有公共Invoke方法。 F#定义了与声明类型具有相同可见性的Invoke方法。 当Expression.Lambda中的代码尝试在给定的委托类型中查找Invoke方法时,它只查找公共方法,假设所有编译器的行为与C#类似 F# and C# use different strategies for emitting Invoke method for delegates. If you try t ...
-
我很懒,所以这段代码只处理公共属性。 但它应该是一个很好的基础,让你开始。 public static string[] Foo
(Expression > func) { var properties = func.Body.Type.GetProperties(); return typeof(T).GetProperties() .Where(p => properties.Any(x => p.Name == x.Name)) ... -
你不能这样做,因为lambda表达式本身没有类型; 它的类型由上下文决定(例如,如果将其分配给委托变量或将其作为参数传递给方法)。 由于((Thing t) => t.Property)没有类型,因此您无法在其上调用扩展方法,因为编译器不知道哪些扩展方法是有效候选。 但是,您可以声明一个变量并在其上调用扩展方法: Func
func = t => t.Property; string name = func.GetName(); You can't do that, ...