我目前正在开发一个项目,其中一部分代码如下所示:
Select Case oReader.Name
Case "NameExample1"
Me.Elements.NameExample1.Value = oReader.ReadString
....
Case "NameExampleN"
Me.Elements.NameExampleN.Value = oReader.ReadString
....
End Select
Run Code Online (Sandbox Code Playgroud)
它会持续一段时间.代码显然很冗长,感觉可以改进.有没有办法动态调用VB.NET中的属性,这样就可以完成这样的事情:
Dim sReadString As String = oReader.ReadString
Me.Elements.InvokeProperty(sReadString).Value = sReadString
Run Code Online (Sandbox Code Playgroud) 我需要动态地将属性转换为其实际类型.我如何/可以使用反射做到这一点?
解释我正在努力的真实场景.我试图在Entity Framework属性上调用"First"扩展方法.要在Framework上下文对象上调用的特定属性作为字符串传递给方法(以及要检索的记录的id).所以我需要对象的实际类型才能调用First方法.
我不能在对象上使用"Where"方法,因为lambda或delegate方法仍然需要对象的实际类型才能访问属性.
此外,由于对象是由实体框架生成的,因此我无法将类型转换为接口并对其进行操作.
这是场景代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;
namespace NmSpc
{
public class ClassA
{
public int IntProperty { get; set; }
}
public class ClassB
{
public ClassA MyProperty { get; set; }
}
public class ClassC
{
static void Main(string[] args)
{
ClassB tester = new ClassB();
PropertyInfo propInfo = typeof(ClassB).GetProperty("MyProperty");
//get a type unsafe reference to ClassB`s property
Object property = propInfo.GetValue(tester, null);
//get the type safe reference to …Run Code Online (Sandbox Code Playgroud) 这是一个测试类:
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
public class TestAnnotations {
@interface Annotate{}
@Annotate public void myMethod(){}
public static void main(String[] args) {
try{
Method[] methods = TestAnnotations.class.getDeclaredMethods();
Method m = methods[1];
assert m.getName().equals("myMethod");
System.out.println("method inspected ? " + m.getName());
Annotation a = m.getAnnotation(Annotate.class);
System.out.println("annotation ? " + a);
System.out.println("annotations length ? "
+ m.getDeclaredAnnotations().length);
}
catch(Exception e){
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的输出:
method inspected ? myMethod
annotation : null
annotations length : 0
Run Code Online (Sandbox Code Playgroud)
我错过了通过反思使注释可见?
我是否需要一个注释处理器,即使只是检查它们的存在?
如何从MethodBase中找出方法的返回类型?我正在使用PostSharp并尝试覆盖CompileTimeValidate(MethodBase方法)方法以确保该属性应用于具有正确签名的方法.
谢谢,
如何通过反射找出方法的字符串名称?
例如给出:
class Car{
public void getFoo(){
}
}
Run Code Online (Sandbox Code Playgroud)
我想得到字符串"getFoo",如下所示:
Car.getFoo.toString() == "getFoo" // TRUE
Run Code Online (Sandbox Code Playgroud) 我正在实例化这样一个类.
myObj = (myObj) Class.forName("fully qualified class name here").newInstance();
Run Code Online (Sandbox Code Playgroud)
我怀疑的是,如果我们有一个构造函数来获取参数,我们如何像上面那样实例化它.
谢谢,
Narendra
使用以下功能:
function is_closure($t) { return ( !is_string($t) && is_callable($t)); }
除了匿名闭包函数之外,这可以为其他任何东西返回true吗?如果是这样,如果变量是闭包,那么确定的正确方法是什么?
非常感谢
我知道我的C#类中的属性名称.是否可以使用反射来设置此属性的值?
例如,假设我知道属性的名称string propertyName = "first_name";.并且存在一个名为的财产first_name.我可以用这个字符串设置它吗?
@在Groovy中的字段名之前是什么意思?对于某些类,我可以访问不能直接访问的私有字段,让我们以ComposedClosure为例:
public class Person {
private String name
}
def u = new Person(name:"Ron")
println u.@name //Ron
println u.name //Ron
a = {2} >> {3}
println a.@first //first closure object
println a.first //runtime error
Run Code Online (Sandbox Code Playgroud) 我知道我们可以通过反射来访问私有构造函数@Sanjay T. Sharma在他的问题回答中提到:"instanceof Void"总是返回false吗?
你可以使用反射访问私有 - 方法,构造函数,数据成员,一切.