标签: reflection

如何使用反射获取变量名称?

例如,

static void Main()
{
    var someVar = 3;

    Console.Write(GetVariableName(someVar));
}
Run Code Online (Sandbox Code Playgroud)

该程序的输出应为:

someVar
Run Code Online (Sandbox Code Playgroud)

如何使用反射实现这一目标?

c# reflection

38
推荐指数
1
解决办法
4万
查看次数

从类型的部分名称获取System.Type

我想得到一个System.Type给定的类型名称string.

例如,如果我有一个对象:

MyClass abc = new MyClass();
Run Code Online (Sandbox Code Playgroud)

我可以说:

System.Type type = abc.GetType();
Run Code Online (Sandbox Code Playgroud)

但如果我拥有的是:

string className = "MyClass";
Run Code Online (Sandbox Code Playgroud)

.net c# reflection types

37
推荐指数
2
解决办法
3万
查看次数

使用lambda表达式获取属性名称和类型

我正在尝试编写一个函数,它将使用如下语法提取属性的名称和类型:

private class SomeClass
{
    Public string Col1;
}

PropertyMapper<Somewhere> propertyMapper = new PropertyMapper<Somewhere>();
propertyMapper.MapProperty(x => x.Col1)
Run Code Online (Sandbox Code Playgroud)

有没有办法将属性传递给函数而不对此语法进行任何重大更改?

我想获取属性名称和属性类型.

所以在下面的例子中我想要检索

Name = "Col1" 和 Type = "System.String"

有人可以帮忙吗?

c# reflection lambda

37
推荐指数
1
解决办法
3万
查看次数

如何使用反射确定属性类型?

我如何测试类型的属性以查看它是否是指定的类型?

编辑:我的目标是检查程序集,以查看该程序集中的任何类型是否包含MyType属性(或从MyType继承).

这是我走下去的赛道......

AssemblyName n = new AssemblyName();
n.CodeBase = "file://" + dllName;
Assembly a = AppDomain.CurrentDomain.Load(n);

foreach (Type t in a.GetTypes())
    foreach (PropertyInfo pi in t.GetProperties())
        if ( pi.PropertyType is MyType ) // warning CS0184
            Console.WriteLine("Found a property that is MyType");
Run Code Online (Sandbox Code Playgroud)

这会编译警告CS0184:给定的表达式永远不是提供的('MyType')类型

.net c# reflection

37
推荐指数
2
解决办法
6万
查看次数

使用反射获取类方法

当类名作为字符串传递时,如何使用反射获取类的所有公共方法,如下面的方法所示.?

 private  MethodInfo[] GetObjectMethods(string selectedObjClass)
 {
   MethodInfo[] methodInfos;
   Assembly assembly = Assembly.GetAssembly(typeof(sampleAdapater));
   Type _type = assembly.GetType("SampleSolution.Data.MyData." + selectedObjClass);

  ///get all the methods for the classname passed as string

   return methodInfos;

 }
Run Code Online (Sandbox Code Playgroud)

请帮忙.谢谢

c# reflection

37
推荐指数
2
解决办法
5万
查看次数

C#反射并找到所有引用

给定一个DLL文件,我希望能够找到该DLL文件中所有方法的调用.我怎样才能做到这一点?

从本质上讲,我如何以编程方式执行Visual Studio已经执行的操作?

我不想使用像.NET Reflector这样的工具来做到这一点,但反射很好,可能是必要的.

c# reflection reflector reference

37
推荐指数
2
解决办法
3万
查看次数

Java寻找具有特定注释及其注释元素的方法

假设我有这个注释类


@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MethodXY {
    public int x();
    public int y();
}

public class AnnotationTest {
    @MethodXY(x=5, y=5)
    public void myMethodA(){ ... }

    @MethodXY(x=3, y=2)
    public void myMethodB(){ ... }
}
Run Code Online (Sandbox Code Playgroud)

那么有没有办法查看一个对象,使用@MethodXY注释"寻找"该方法,其中元素x = 3,y = 2,并调用它?

谢谢

java reflection annotations

37
推荐指数
1
解决办法
5万
查看次数

如何使用MethodInfo.Invoke传递参数作为引用

如何将参数作为参考传递MethodInfo.Invoke?

这是我想要调用的方法:

private static bool test(string str, out byte[] byt)
Run Code Online (Sandbox Code Playgroud)

我尝试了这个,但我失败了:

byte[] rawAsm = new byte[]{};
MethodInfo _lf = asm.GetTypes()[0].GetMethod("test", BindingFlags.Static |  BindingFlags.NonPublic);
bool b = (bool)_lf.Invoke(null, new object[]
{
    "test",
    rawAsm
});
Run Code Online (Sandbox Code Playgroud)

返回的字节为空.

c# reflection methodinfo

37
推荐指数
2
解决办法
4万
查看次数

通过反射确定属性是否是一种数组

如何确定属性是否是一种数组.

例:

public bool IsPropertyAnArray(PropertyInfo property)
{
    // return true if type is IList<T>, IEnumerable<T>, ObservableCollection<T>, etc...
}
Run Code Online (Sandbox Code Playgroud)

c# reflection

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

如何从C#ExpandoObject中按名称动态获取属性?

我有一个ExpandoObject并且想要为它创建一个getter,它将在运行时按名称返回一个属性,其中名称以字符串形式指定而不是硬编码.

例如,我可以这样做:

account.features.isEmailEnabled;
Run Code Online (Sandbox Code Playgroud)

这将返回真实. account是一个ExpandoObject,features也是一个ExpandoObject.所以我有一个ExpandoObject包含其他的ExpandoObjects.

所以我想要做的是:

account.features.GetProperty("isEmailEnabled");
Run Code Online (Sandbox Code Playgroud)

并让它返回真实.

原因是我有很多功能,我希望能够编写一个通用的getter方法,我可以在其中传递我想要的功能的名称,该方法将传递给我的值为account.features.whatever(通过将字符串传递给泛型getter方法指定"whatever".否则,我将不得不为每个功能写一个30个吸气剂.

我做了很多研究并尝试过这样的事情:

var prop = account.features.GetType();  
// this returns System.Dyanmic.ExpandoObject
Run Code Online (Sandbox Code Playgroud)

其次是

var value = prop.GetProperty(featureNameAsString); 
Run Code Online (Sandbox Code Playgroud)

但value总是回来为null.我不明白为什么.在观察窗口我可以做account.features.isEmailEnabled,它显示为真,并说它是一个布尔值.但是,如果我尝试使用上面的方法获得此值并传入,isEmailEnabled因为featureNameAsString我只是得到null.

有人可以告诉我,我可能做错了什么,什么是好方法,而不是太复杂?

我在4.5.1框架下使用ASP.NET.

c# asp.net oop reflection dynamic

37
推荐指数
1
解决办法
3万
查看次数

标签 统计

reflection ×10

c# ×9

.net ×2

annotations ×1

asp.net ×1

dynamic ×1

java ×1

lambda ×1

methodinfo ×1

oop ×1

reference ×1

reflector ×1

types ×1