标签: reflection

Python反射 - 我可以使用它来获取方法定义的源代码

重复..


我有一个成功运行的方法定义,但想在运行时修改它.

例如:如果我有方法

def sayHello():
    print "Hello"
Run Code Online (Sandbox Code Playgroud)

type(sayHello)给我答案'type function'.我能获取此函数对象的源代码字符串吗?它被认为是安全问题吗?

python reflection

10
推荐指数
3
解决办法
6350
查看次数

设置和获取使用反射的性能

是否设置和使用反射(得到一个对象的属性java.lang.reflect.Field get和set功能),而不是调用set和get对象本身,导致任何显著的性能差异?

java reflection

10
推荐指数
1
解决办法
4171
查看次数

如何从封闭类型的MethodInfo获取开放泛型类型的MethodInfo

假设我有一个这样的类:

public class MyClass<T>
{
    public void Foo(T t)
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,假设我有一个实例MyClass<int>和MethodInfo它的Foo方法.调用methodInfo.GetParameters()将返回一个ParameterInfo带有一个条目的数组,引用类型int.我的问题是,我似乎无法找到,如果该参数被声明为int在类中或作为T.

我想要实现的目标是什么?
在运行时,我想阅读MethodInfoVisual Studio生成的XML Doc文件中指定的方法的文档.
对于上面定义的方法,键如下所示:

<namespace>.MyClass`1.Foo(`0)
Run Code Online (Sandbox Code Playgroud)

该`0指声明类的第一个泛型类型参数.为了能够构造这个字符串,我需要以某种方式获取此信息.
但是怎么样? MethodInfo似乎没有包含该信息......

.net c# reflection

10
推荐指数
1
解决办法
1613
查看次数

获取具有特定值的自定义属性的所有属性

可能重复:
如何获取具有给定属性的属性列表?

我有这样的自定义类

public class ClassWithCustomAttributecs
{
    [UseInReporte(Use=true)]
    public int F1 { get; set; }

    public string F2 { get; set; }

    public bool F3 { get; set; }

    public string F4 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我有一个自定义属性UseInReporte:

[System.AttributeUsage(System.AttributeTargets.Property ,AllowMultiple = true)]
public class UseInReporte : System.Attribute
{
    public bool Use;

    public UseInReporte()
    {
        Use = false;
    }
}
Run Code Online (Sandbox Code Playgroud)

不,我想获得所有具有[UseInReporte(Use=true)]如何使用反射做到这一点的属性?

谢谢

c# reflection attributes data-annotations c#-4.0

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

Roslyn vs反射TypeScript代码生成器

我正在开发一个TypeScript代码生成器,它将在C#类上使用自定义属性来生成TypeScript定义和代码文件.

我正在考虑TypeScript代码生成/源文件分析的两个选项:

  • 对编译程序集的反思
  • 罗斯林CTP

该工具将使用属性和方法上的自定义属性来生成TypeScript文件.现在我不打算将C#方法体转换为JavaScript,但将来可能会这样做.因此,我正在认真考虑罗斯林.但是,为了简单地生成我的TypeScript类的轮廓,我想我可以使用反射和自定义属性.

我想知道:

a)Roslyn是否提供了Reflection无法实现的功能?我的理解是我无法使用Reflection获取方法体.

b)Roslyn CTP许可证会阻止我在开源许可下分发该工具吗?阅读许可证后,我不清楚这一点

.net c# reflection roslyn typescript

10
推荐指数
1
解决办法
2654
查看次数

使用Reflection的C#中索引属性的SetValue

我有一个班级有一个

ObservableCollection<int>
Run Code Online (Sandbox Code Playgroud)

作为属性,我正在尝试更改该类实例的属性内的值.这是我的代码,并且得到了一个TargetException:

object[] index = null;
var originalPropertyName = propertyName;
if (propertyName.Contains("[") && propertyName.Contains("]"))
{
    index = new object[1];
    index[0] = Convert.ToInt32(propertyName.Split('[')[1].Split(']')[0]);
    propertyName = propertyName.Split('[')[0];
}
PropertyInfo pi = item.GetType().GetProperty(propertyName);
PropertyInfo opi = item.GetType().GetProperty(originalPropertyName);
Type pType = index != null ? pi.PropertyType.GetGenericArguments()[0] : pi.PropertyType;
if (pi != null)
{
    object convertedValue = Convert.ChangeType(value, pType);
    if (index == null)
    {
        item.GetType().GetProperty(propertyName).SetValue(item, convertedValue, null);
    }
    else
    {
        //PropertyInfo ipi = pi.PropertyType.GetProperties().Single(p => p.GetIndexParameters().Length > 0);
        //var collection = pi.GetValue(item, index);
        //collection.GetType().GetProperty("Value").SetValue(collection, …
Run Code Online (Sandbox Code Playgroud)

c# reflection

10
推荐指数
1
解决办法
4792
查看次数

设计时间反思

有没有办法在设计时进行预编译反射?

我的意图是使用T4基于实现某些接口的类来吐出自定义代码.我知道我可以调用反射,但我希望T4脚本在编译之前吐出额外的代码,否则我需要编译代码两次,一次生成dll,两次让T4反映先前生成的dll并添加额外的脚手架.

有没有办法在设计时做反思?

有一个更好的方法吗?

.net reflection t4 design-time visual-studio

10
推荐指数
2
解决办法
3981
查看次数

使用Reflection确定哪些字段是属性的后备字段

我正在使用反射来绘制对象.这些对象在托管代码中,但除了通过反射之外,我无法看到它们的源代码,底层结构等.所有这一切的首要目标是对象的基本内存映射(类似于SOS.dll DumpObject和!ObjSize命令的功能).因此,我试图确定哪些成员被"重复计算"为一个字段和一个属性.

例如:

public class CalendarEntry
{
    // private property 
    private DateTime date { get; set;}

    // public field 
    public string day = "DAY";
}
Run Code Online (Sandbox Code Playgroud)

映射时显示:

  • 字段
    • 天
    • k__BackingField
  • 属性
    • 日期

像这样的一个类:

public class CalendarEntry
{
    // private field 
    private DateTime date;

    // public field 
    public string day = "DAY";

    // Public property exposes date field safely. 
    public DateTime Date
    {
        get
        {
            return date;
        }
        set
        {
                date = value;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

映射时显示:

  • 字段
    • 天 …

c# reflection field properties memory-consumption

10
推荐指数
1
解决办法
3523
查看次数

从类型动态获取类属性值

我正在尝试编写一个方法,在具有特定自定义属性的程序集中查找所有类型.我还需要能够提供匹配的字符串值.需要注意的是,我希望能够在任何类上运行它并返回任何值.

例如:我想执行这样的调用

Type tTest = TypeFinder.GetTypesWithAttributeValue(Assembly.Load("MyAssembly"), typeof(DiagnosticTestAttribute), "TestName", "EmailTest");
Run Code Online (Sandbox Code Playgroud)

到目前为止我的方法看起来像这样:

public static Type GetTypesWithAttributeValue(Assembly aAssembly, Type tAttribute, string sPropertyName, object oValue)
{
    object oReturn = null;
    foreach (Type type in aAssembly.GetTypes())
    {
        foreach (object oTemp in type.GetCustomAttributes(tAttribute, true))
        {
            //if the attribute we are looking for matches
            //the value we are looking for, return the current type.
        }
    }
    return typeof(string); //otherwise return a string type
}
Run Code Online (Sandbox Code Playgroud)

我的属性看起来像这样:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
public class DiagnosticTestAttribute : Attribute
{
    private string _sTestName …
Run Code Online (Sandbox Code Playgroud)

c# reflection custom-attributes linq-expressions

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

使用GSON反序列化嵌套的泛型类时的奇怪行为

我正在编写一个将连接到服务器并基于某些参数的类,检索一个json-string,该字符串将使用GSON解析为指定的(通过泛型)类.

这个课程的精简版看起来像这样:

class Executor<T> {

    private Response<T> response;

    public void execute() {
        Type responseType = new TypeToken<Response<T>>() {}.getType();
        this.response = new Gson().fromJson(json, responseType);
    }

    public Response<T> getResponse() { return this.response; }

}
Run Code Online (Sandbox Code Playgroud)

(JSON变量看起来像这样.)

一旦反序列化存储数据的类如下所示:

class Response<T> {

    private List<T> data = null;

    public List<T> getData() { return this.data; }

}
Run Code Online (Sandbox Code Playgroud)

数据尝试反序列化的类:

public class Language {
    public String alias;
    public String label;
}
Run Code Online (Sandbox Code Playgroud)

运行的代码使用上面的类:

Executor<Language> executor = new Executor<Language();
List<Language> languages = executor.execute().getResponse().getData();
System.out.println(languages.get(0).alias); // exception occurs …
Run Code Online (Sandbox Code Playgroud)

java oop generics reflection gson

10
推荐指数
1
解决办法
7503
查看次数