我正在解析表达式树.给定ExpressionType.MemberAccess的NodeType,如何获取该Field的值?
来自C#MSDN文档:MemberAccess是表示从字段或属性读取的节点.
代码片段令人难以置信,非常有用.提前致谢!!!
我的代码看起来像这样:
public static List<T> Filter(Expression<Func<T, bool>> filterExp)
{
//the expression is indeed a binary expression in this case
BinaryExpression expBody = filterExp.Body as BinaryExpression;
if (expBody.Left.NodeType == ExpressionType.MemberAccess)
//do something with ((MemberExpressionexpBody.Left).Name
//right hand side is indeed member access. in fact, the value comes from //aspdroplist.selectedvalue
if (expBody.Right.NodeType == ExpressionType.MemberAccess)
{
//how do i get the value of aspdroplist.selected value?? note: it's non-static
}
//return a list
}
Run Code Online (Sandbox Code Playgroud) 我想使用反射来设置私有字段的值以进行单元测试.
问题是,该字段是静态的.
这是我正在做的工作:
/**
* Use to set the value of a field you don't have access to, using reflection, for unit testing.
*
* Returns true/false for success/failure.
*
* @param p_instance an object to set a private field on
* @param p_fieldName the name of the field to set
* @param p_fieldValue the value to set the field to
* @return true/false for success/failure
*/
public static boolean setPrivateField(final Object p_instance, final String p_fieldName, final Object p_fieldValue) { …Run Code Online (Sandbox Code Playgroud) 我试图解决与动态枚举查找相关的编译错误("绑定不匹配:...").
基本上我想要实现这样的事情:
String enumName = whatever.getEnumName();
Class<? extends Enum<?>> enumClass = whatever.getEnumClass();
Enum<?> enumValue = Enum.valueOf(enumClass, enumName);
Run Code Online (Sandbox Code Playgroud)
无论我做什么,我总是最终得到编译错误.老实说,仿制药和枚举对我来说是非常令人难以置信的......
我在这做错了什么?
我将在不传递属性的任何参数的情况下执行此操作!可能吗?
class MyAtt : Attribute {
string NameOfSettedProperty() {
//How do this? (Would be MyProp for example)
}
}
class MyCls {
[MyAtt]
int MyProp { get { return 10; } }
}
Run Code Online (Sandbox Code Playgroud) 可能是一个愚蠢的问题,但我在网上找不到任何解释.
这段代码不起作用的具体原因是什么?该代码应该将属性值从Contact(源)复制到新实例化的ContactBO(目标)对象.
public ContactBO(Contact contact)
{
Object source = contact;
Object destination = this;
PropertyInfo[] destinationProps = destination.GetType().GetProperties(
BindingFlags.Public);
PropertyInfo[] sourceProps = source.GetType().GetProperties(
BindingFlags.Public);
foreach (PropertyInfo currentProperty in sourceProps)
{
var propertyToSet = destinationProps.First(
p => p.Name == currentProperty.Name);
if (propertyToSet == null)
continue;
try
{
propertyToSet.SetValue(
destination,
currentProperty.GetValue(source, null),
null);
}
catch (Exception ex)
{
continue;
}
}
}
Run Code Online (Sandbox Code Playgroud)
两个类都具有相同的属性名称(BO类有一些其他类,但它们在初始化时无关紧要).这两个类都只有公共属性.当我运行上面的例子,destinationProps 并且 sourceProps长度为零.
但是当我扩展GetProperties方法时BindingFlags.Instance,它会突然返回所有内容.我很感激,如果有人可以解释这件事,因为我迷路了.
我有一个myobject可能会返回的对象None.如果返回None,则不会返回属性id:
a = myobject.id
Run Code Online (Sandbox Code Playgroud)
所以当myobject是None,上面的结果导致AttributeError:
AttributeError: 'NoneType' object has no attribute 'id'
Run Code Online (Sandbox Code Playgroud)
如果myobject是None,那么我想a等于None.如何在一行语句中避免此异常,例如:
a = default(myobject.id, None)
Run Code Online (Sandbox Code Playgroud) 我知道如果我们知道注释类,我们可以轻松获取特定的注释并访问它的属性.例如:
field.getAnnotation(Class<T> annotationClass)
Run Code Online (Sandbox Code Playgroud)
这将返回特定注释界面的引用,因此您可以轻松访问它的值.
我的问题是,如果我对特定的注释类没有预先知识.我只想在运行时使用反射来获取所有注释类名及其属性,以便将类信息转储为例如json文件.我怎么能以简单的方式做到这一点.
Annotation[] field.getAnnotations();
Run Code Online (Sandbox Code Playgroud)
此方法仅返回注释接口的动态代理.
Java 8能够使用Reflection API获取方法参数名称.
如何获取这些方法参数名称?
据我所知,类文件不存储正式的参数名称.我如何使用反射获得这些?
是否可以将对象强制转换为返回的类型GetType()?我想要一个可以接受对象(对于匿名类型)的泛型方法,但然后返回一个对象强制转换为匿名类型.我一直在考虑使用LCG DynamicMethod在容器类上构建一个方法,但我无法弄清楚它会是什么样子.使用该GetType()方法进行强制转换的想法是能够获取匿名类型并将对象强制转换为其实际类型,而无需实际知道类型.
最重要的目标是将匿名类型的对象粘贴到容器中,然后我可以在方法之间共享和传递.
reflection ×10
java ×5
c# ×4
attributes ×2
annotations ×1
casting ×1
default ×1
enums ×1
field ×1
generics ×1
java-8 ×1
protected ×1
python ×1
unit-testing ×1
void ×1