标签: reflection

为什么我的C#IS语句不起作用?

我有以下代码,其中T是通用定义如下:

public abstract class RepositoryBase<T> where T : class, IDataModel
Run Code Online (Sandbox Code Playgroud)

这段代码工作得很好:

PropertyInfo propertyInfo = typeof(T).GetProperty(propertyName);
if (propertyInfo.DeclaringType.FullName == typeof(T).FullName)  <--- Works just fine
Run Code Online (Sandbox Code Playgroud)

vs这个评估为false的代码

PropertyInfo propertyInfo = typeof(T).GetProperty(propertyName);
if (propertyInfo.DeclaringType is T) <-- does not work
Run Code Online (Sandbox Code Playgroud)

我在这做错了什么?

c# reflection properties

9
推荐指数
1
解决办法
1363
查看次数

C#Reflection-查找类是否定义了析构函数

我试图找出一个类是否有使用反射的析构函数.我确实看到了在System.Reflection中获取构造函数的方法.有没有办法找到一个类是否在C#中定义了自定义析构函数?

c# reflection destructor

9
推荐指数
1
解决办法
848
查看次数

反射无法在抽象类的属性上找到私有的setter

当我在抽象类中拥有此属性时:

public IList<Component> Components { get; private set; }
Run Code Online (Sandbox Code Playgroud)

然后当我打电话:

p.GetSetMethod(true)
Run Code Online (Sandbox Code Playgroud)

如果p是指向我的属性的PropertyInfo对象,则为null.

但是,如果我将属性setter更改为protected,我可以通过反射看到它.为什么是这样?我似乎没有回忆起非抽象类的这个问题......

.net c# reflection

9
推荐指数
2
解决办法
1769
查看次数

使用java 8 lambda表达式打印有关错误的调试信息

我想使用静态方法作为setter helper来捕获异常并打印有关失败操作的调试信息.我不希望只有例外细节.我想显示正在设置的属性,以便详细帮助快速调试问题.我正在使用Java 8.

我应该如何提供或检测所设置的财产?

我希望删除示例中的"名称"字符串并获得相同的结果.

我知道我不能对提供的提供的setter方法使用反射,该方法转换为lambda表达式然后转换为BiConsumer.

我得到了这个,但需要提供属性名称.

/** setter helper method **/
private static <E, V> void set(E o, BiConsumer<E, V> setter,
        Supplier<V> valueSupplier, String propertyName) {
    try {
        setter.accept(o, valueSupplier.get());
    } catch (RuntimeException e) {
        throw new RuntimeException("Failed to set the value of " + propertyName, e);
    }
}
Run Code Online (Sandbox Code Playgroud)

例:

    Person p = new Person();
    Supplier<String> nameSupplier1 = () ->  "MyName";
    Supplier<String> nameSupplier2 = () -> { throw new RuntimeException(); };
    set(p, Person::setName, nameSupplier1, "name");
    System.out.println(p.getName()); // prints MyName …
Run Code Online (Sandbox Code Playgroud)

java reflection debugging lambda java-8

9
推荐指数
1
解决办法
2615
查看次数

使用反射来调用字段上的方法

我的代码如下所示:

class MyObject {

    MyField f = new MyField();

}

class MyField {
    public void greatMethod();
}
Run Code Online (Sandbox Code Playgroud)

有没有办法greatMethod()在类的对象上调用using反射MyObject?

我尝试了以下方法:

Field f = myObject.getClass().getDeclaredField("f");
Method myMethod = f.getDeclaringClass().getDeclaredMethod("greatMethod", new Class[]{});
myMethod.invoke(f);
Run Code Online (Sandbox Code Playgroud)

但是它试图greatMethod()直接调用我的myObject,而不是调用其中的字段f.有没有办法实现这一点,而无需修改MyObject类(因此它将实现一个在f上调用适当方法的方法).

java reflection methods invoke

9
推荐指数
1
解决办法
8250
查看次数

Reflect.Value.FieldByName导致恐慌

我在调用反射值的.FieldByName方法时遇到以下错误,确切的错误是: -

panic: reflect: call of reflect.Value.FieldByName on ptr Value
Run Code Online (Sandbox Code Playgroud)

而代码是: -

s := reflect.ValueOf(&value).Elem() (value is a struct)
metric := s.FieldByName(subval.Metric).Interface() (subval.Metric is a string)
Run Code Online (Sandbox Code Playgroud)

我知道这并不多,但这是我能得到的所有信息.

这是Go Playground代码的链接:http://play.golang.org/p/E038cPOoGp

reflection go

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

Scala注释继承

在Scala中,我有一个注释和带有注释的基本特征,但扩展该类不会继承注释:

scala> import scala.annotation.StaticAnnotation
import scala.annotation.StaticAnnotation

scala> case class AnnotationClass() extends StaticAnnotation
defined class AnnotationClass

scala> @AnnotationClass trait BaseTrait
defined trait BaseTrait

scala> class InheritingClass extends BaseTrait
defined class InheritingClass

scala> import scala.reflect.runtime.universe._
import scala.reflect.runtime.universe._

scala> typeOf[BaseTrait].typeSymbol.asClass.annotations.size
res1: Int = 1

scala> typeOf[InheritingClass].typeSymbol.asClass.annotations.size
res0: Int = 0
Run Code Online (Sandbox Code Playgroud)

有没有办法让子类继承父级的注释?

reflection inheritance annotations scala

9
推荐指数
1
解决办法
747
查看次数

如何根据表名在DbContext中选择正确的DbSet

假设我有一个带有以下DbSet的DbContext

class Amimals : DbContext
{
    public DbSet<Dog> Dogs { get; set; }
    public DbSet<Cat> Cats { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

在每个EntityTypeConfiguration内部,我为每个DbSet定义表

class DogConfig : EntityTypeConfiguration
{
    public  DogConfig()
    {
        this.ToTable("DOG_TABLE");
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,如果我有表名和DbContext,我怎样才能获取并使用正确的DbSet?

void foo()
{
    string tableName = this.GetTableName();
    using(Animals context = new Animals())
    {
        /* Made up solution */
        DbSet animalContext = context.Where(c => c.TableName == tableName);
        ...
        /* Do something with DbSet */
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

c# linq reflection entity-framework

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

在非泛型方法中使用反射等待Task <TDerived>的结果

考虑以下情况:

class A
{
    public int Id;
}

class B : A
{

}

class Main
{
    public async Task<int> Create(Type type)
    {
        MethodInfo method = this.GetType().GetMethod("Create", new Type[] { typeof(string) }).MakeGenericMethod(new Type[] { type });
        A a = await (Task<A>)method.Invoke(this, new object[] { "humpf" });
        return a.Id;
    }

    public async Task<T> Create<T>(string name) where T : A
    {
        T t = await Foo.Bar<T>(name);
        return t;
    }
}
Run Code Online (Sandbox Code Playgroud)

呼叫new Main().Create(typeof(B))将失败

无法将' System.Threading.Tasks.Task[B]'类型的对象强制转换为' System.Threading.Tasks.Task[A]'

我不太明白,因为在这种情况下,Generic Create<T>方法只返回始终从' …

c# generics reflection async-await

9
推荐指数
1
解决办法
2347
查看次数

如何使用Reflection API在TypeScript中获取数组项类型?

我在TypeScript中有以下小类,其中一些公共字段被装饰:

class Company {
    @dataMember
    public name: string;

    @dataMember
    public people: Person[];
}

class Person {
    // ...
}
Run Code Online (Sandbox Code Playgroud)

通过使用反射元数据,我可以确定公司属性名称和人员的类型:它们分别是构造函数String和Array,它们是预期的和逻辑的.

我的属性装饰函数:

function decorate(target: Object, propertyKey: string | symbol): void {
    var reflectType = Reflect.getMetadata("design:type", target, propertyKey);
    // ...
}
Run Code Online (Sandbox Code Playgroud)

但是我如何确定数组元素的类型(构造函数)?它甚至可能吗?在上面的例子中,它应该是(引用)Person.


注意:我在实例化之前需要类型引用,因此,使用数组项动态确定类型是不可能的:没有数组项,甚至没有Array实例.

arrays reflection decorator typescript

9
推荐指数
1
解决办法
2625
查看次数