(参见下面我使用我接受的答案创建的解决方案)
我正在尝试提高一些涉及反射的代码的可维护性.该应用程序有一个.NET Remoting接口,公开(除此之外)一个名为Execute的方法,用于访问未包含在其已发布的远程接口中的应用程序部分.
以下是应用程序如何指定可通过Execute访问的属性(本例中为静态属性):
RemoteMgr.ExposeProperty("SomeSecret", typeof(SomeClass), "SomeProperty");
Run Code Online (Sandbox Code Playgroud)
所以远程用户可以调用:
string response = remoteObject.Execute("SomeSecret");
Run Code Online (Sandbox Code Playgroud)
并且应用程序将使用反射来查找SomeClass.SomeProperty并将其值作为字符串返回.
不幸的是,如果有人重命名SomeProperty并忘记更改ExposeProperty()的第3个parm,它会破坏这种机制.
我需要相当于:
SomeClass.SomeProperty.GetTheNameOfThisPropertyAsAString()
Run Code Online (Sandbox Code Playgroud)
在ExposeProperty中用作第三个parm,因此重构工具将负责重命名.
有没有办法做到这一点?提前致谢.
好的,这是我最终创建的内容(根据我选择的答案和他引用的问题):
// <summary>
// Get the name of a static or instance property from a property access lambda.
// </summary>
// <typeparam name="T">Type of the property</typeparam>
// <param name="propertyLambda">lambda expression of the form: '() => Class.Property' or '() => object.Property'</param>
// <returns>The name of the property</returns>
public string GetPropertyName<T>(Expression<Func<T>> propertyLambda)
{
var me = propertyLambda.Body as MemberExpression;
if (me == null)
{ …Run Code Online (Sandbox Code Playgroud) 我试图通过参数反射调用方法,我得到:
对象与目标类型不匹配
如果我调用没有参数的方法,它可以正常工作.如果我调用该方法Test("TestNoParameters"),则基于以下代码,它可以正常工作.但是,如果我打电话Test("Run"),我会得到一个例外.我的代码有问题吗?
我最初的目的是传递一系列对象,例如public void Run(object[] options)但这不起作用,我尝试了一些更简单的例如字符串而没有成功.
// Assembly1.dll
namespace TestAssembly
{
public class Main
{
public void Run(string parameters)
{
// Do something...
}
public void TestNoParameters()
{
// Do something...
}
}
}
// Executing Assembly.exe
public class TestReflection
{
public void Test(string methodName)
{
Assembly assembly = Assembly.LoadFile("...Assembly1.dll");
Type type = assembly.GetType("TestAssembly.Main");
if (type != null)
{
MethodInfo methodInfo = type.GetMethod(methodName);
if (methodInfo != null)
{
object result = null;
ParameterInfo[] …Run Code Online (Sandbox Code Playgroud) 我想调用main静态的方法.我得到了类型的对象Class,但我无法创建该类的实例,也无法调用该static方法main.
C#的异常类有一个source属性,默认情况下设置为程序集的名称.
有没有另一种方法来获得这个确切的字符串(没有解析不同的字符串)?
我尝试过以下方法:
catch(Exception e)
{
string str = e.Source;
//"EPA" - what I want
str = System.Reflection.Assembly.GetExecutingAssembly().FullName;
//"EPA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
str = typeof(Program).FullName;
//"EPA.Program"
str = typeof(Program).Assembly.FullName;
//"EPA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
str = typeof(Program).Assembly.ToString();
//"EPA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
str = typeof(Program).AssemblyQualifiedName;
//"EPA.Program, EPA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
}
Run Code Online (Sandbox Code Playgroud) 我正在玩Java的反射API并试图处理一些字段.现在我一直在确定我的领域类型.字符串很简单,就是这样myField.getType().equals(String.class).这同样适用于其他非派生类.但是我如何检查派生类?例如LinkedList作为的子类List.我找不到任何isSubclassOf(...)或extends(...)方法.我是否需要getSuperClass()走遍所有人并自己找到我的supeclass?
使用Java反射时,我对getFields方法和getDeclaredFields方法之间的区别感到有些困惑.
我读过,它getDeclaredFields允许您访问该类的所有字段,并且 getFields只返回公共字段.如果是这种情况,你为什么不经常使用getDeclaredFields?
有人可以详细说明这一点,并解释两种方法之间的区别,以及何时/为什么要使用一种方法?
使用时是否存在偏好或行为差异:
if(obj.getClass().isArray()) {}
Run Code Online (Sandbox Code Playgroud)
和
if(obj instanceof Object[]) {}
Run Code Online (Sandbox Code Playgroud)
?
使用反射创建对象而不是调用类构造函数会导致任何显着的性能差异吗?
我上课了.
Public Class Foo
Private _Name As String
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property
Private _Age As String
Public Property Age() As String
Get
Return _Age
End Get
Set(ByVal value As String)
_Age = value
End Set
End Property
Private _ContactNumber As String
Public Property ContactNumber() As String
Get
Return _ContactNumber
End Get
Set(ByVal value As String)
_ContactNumber = value
End Set
End Property
End …Run Code Online (Sandbox Code Playgroud) reflection ×10
java ×5
c# ×3
.net ×2
class ×2
properties ×2
arrays ×1
assemblyinfo ×1
invoke ×1
methods ×1
optimization ×1
parameters ×1
performance ×1
ruby ×1
static ×1
vb.net ×1