标签: reflection

获取属性的名称作为字符串

(参见下面我使用我接受的答案创建的解决方案)

我正在尝试提高一些涉及反射的代码的可维护性.该应用程序有一个.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)

c# reflection properties

191
推荐指数
9
解决办法
20万
查看次数

获取当前正在执行的方法的名称

$0 是顶级Ruby程序的变量,但当前方法有一个吗?

ruby reflection metaprogramming

190
推荐指数
4
解决办法
6万
查看次数

反思:如何使用参数调用方法

我试图通过参数反射调用方法,我得到:

对象与目标类型不匹配

如果我调用没有参数的方法,它可以正常工作.如果我调用该方法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)

c# reflection parameters methods invoke

187
推荐指数
7
解决办法
35万
查看次数

使用反射调用静态方法

我想调用main静态的方法.我得到了类型的对象Class,但我无法创建该类的实例,也无法调用该static方法main.

java reflection static

185
推荐指数
4
解决办法
14万
查看次数

获取程序集名称

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)

.net c# reflection assemblyinfo

176
推荐指数
3
解决办法
15万
查看次数

检查类对象是否是Java中另一个类对象的子类

我正在玩Java的反射API并试图处理一些字段.现在我一直在确定我的领域类型.字符串很简单,就是这样myField.getType().equals(String.class).这同样适用于其他非派生类.但是我如何检查派生类?例如LinkedList作为的子类List.我找不到任何isSubclassOf(...)extends(...)方法.我是否需要getSuperClass()走遍所有人并自己找到我的supeclass?

java reflection class

174
推荐指数
4
解决办法
12万
查看次数

Java反射中的getFields和getDeclaredFields有什么区别

使用Java反射时,我对getFields方法和getDeclaredFields方法之间的区别感到有些困惑.

我读过,它getDeclaredFields允许您访问该类的所有字段,并且 getFields只返回公共字段.如果是这种情况,你为什么不经常使用getDeclaredFields

有人可以详细说明这一点,并解释两种方法之间的区别,以及何时/为什么要使用一种方法?

java reflection

174
推荐指数
4
解决办法
8万
查看次数

Java数组反射:isArray与instanceof

使用时是否存在偏好或行为差异:

if(obj.getClass().isArray()) {}
Run Code Online (Sandbox Code Playgroud)

if(obj instanceof Object[]) {}
Run Code Online (Sandbox Code Playgroud)

java arrays reflection

173
推荐指数
4
解决办法
12万
查看次数

Java反射性能

使用反射创建对象而不是调用类构造函数会导致任何显着的性能差异吗?

java reflection optimization performance

168
推荐指数
10
解决办法
10万
查看次数

如何遍历类的所有属性?

我上课了.

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)

.net vb.net reflection properties class

165
推荐指数
4
解决办法
15万
查看次数