标签: reflection

测试Convert.ChangeType是否适用于两种类型

这是关于使用反射转换值的问题的后续行动.将某种类型的对象转换为另一种类型可以这样做:

object convertedValue = Convert.ChangeType(value, targetType);
Run Code Online (Sandbox Code Playgroud)

给定两个Type实例(比如FromType和ToType),有没有办法测试转换是否成功?

我可以写一个像这样的扩展方法:

public static class TypeExtensions
{
    public static bool CanChangeType(this Type fromType, Type toType)
    {
        // what to put here?
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:这就是我现在所拥有的.丑陋,但我还没有看到另一种方式......

bool CanChangeType(Type sourceType, Type targetType)
{
  try
  {
    var instanceOfSourceType = Activator.CreateInstance(sourceType);
    Convert.ChangeType(instanceOfSourceType, targetType);
    return true; // OK, it can be converted
  }
  catch (Exception ex)
  {
    return false;
  }
Run Code Online (Sandbox Code Playgroud)

c# reflection

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

如何使用reflection.emit发出显式接口实现?

请注意以下简单的源代码:

using System;
using System.Linq.Expressions;
using System.Reflection;
using System.Reflection.Emit;

namespace A
{
  public static class Program
  {
    private const MethodAttributes ExplicitImplementation =
      MethodAttributes.Private | MethodAttributes.Virtual | MethodAttributes.Final |
      MethodAttributes.HideBySig | MethodAttributes.NewSlot;
    private const MethodAttributes ImplicitImplementation =
      MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.HideBySig;

    private static Type EmitMyIntfType(ModuleBuilder moduleBuilder)
    {
      var typeBuilder = moduleBuilder.DefineType("IMyInterface",
        TypeAttributes.NotPublic | TypeAttributes.Interface | TypeAttributes.Abstract);
      typeBuilder.DefineMethod("MyMethod", MethodAttributes.Assembly | MethodAttributes.Abstract |
        MethodAttributes.Virtual | MethodAttributes.HideBySig | MethodAttributes.NewSlot,
        typeof(void), new[] { typeof(int) });

      return typeBuilder.CreateType();
    }

    public static void Main()
    {
      var …
Run Code Online (Sandbox Code Playgroud)

.net c# reflection reflection.emit explicit-implementation

27
推荐指数
1
解决办法
7969
查看次数

在scala中通过字符串名称获取对象实例

我需要由字符串名称定义的对象(或"单例对象"或"伴随对象"......除了类之外的任何东西).换句话说,如果我有:

package myPackage
object myObject
Run Code Online (Sandbox Code Playgroud)

...那么有这样的事情:

GetSingletonObjectByName("myPackage.myObject") match {
  case instance: myPackage.myObject => "instance is what I wanted"
}
Run Code Online (Sandbox Code Playgroud)

reflection singleton scala

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

如何使用反射来确定一个类是否是内部的?

正如标题所说,如何使用反射检查类定义是否定义为内部?"typeof(...)"返回下面显示的某些属性,但不返回类是否定义为内部属性.看了谷歌,但我能找到的很多关于使用反射运行内部或受保护方法的文章.这不是我对这种情况感兴趣的方法,而是类定义.

var type = typeof(Customer);
Assert.IsTrue(type.IsClass);
Assert.That(type.IsAbstract, Is.EqualTo(isAbstract));
Assert.That(type.IsPublic, Is.EqualTo(isPublic));
Assert.That(type.IsPublic, Is.EqualTo(isPublic));
Assert.That(type.IsSealed, Is.EqualTo(isSealed));
Assert.That(type.IsSerializable, Is.EqualTo(isSerializable));
Run Code Online (Sandbox Code Playgroud)

c# reflection

27
推荐指数
3
解决办法
5672
查看次数

如何列出所有类的变量

有没有办法在C#中列出类的所有变量(字段).如果是,可能有人给我一些例子如何将它们保存在一个List并获得它们可能使用Anonymous Types(var).

c# reflection

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

如何在java中获取参数的注释?

我是一名新的Java程序员.以下是我的代码:

    public void testSimple1(String lotteryName,
                        int useFrequence,
                        Date validityBegin,
                        Date validityEnd,
                        LotteryPasswdEnum lotteryPasswd,
                        LotteryExamineEnum lotteryExamine,
                        LotteryCarriageEnum lotteryCarriage,
                        @TestMapping(key = "id", csvFile = "lottyScope.csv") xxxxxxxx lotteryScope,
                        @TestMapping(key = "id", csvFile = "lotteryUseCondition.csv") xxxxxxxx lotteryUseCondition,
                        @TestMapping(key = "id", csvFile = "lotteryFee.csv") xxxxxxxx lotteryFee)
Run Code Online (Sandbox Code Playgroud)

我想获得所有提交的注释.有些字段是注释的,有些则不是.

我知道如何使用method.getParameterAnnotations()函数,但它只返回三个注释.

我不知道如何对应它们.

我期待以下结果:

lotteryName - none
useFrequence- none
validityBegin -none
validityEnd -none
lotteryPasswd -none
lotteryExamine-none
lotteryCarriage-none
lotteryScope - @TestMapping(key = "id", csvFile = "lottyScope.csv")
lotteryUseCondition - @TestMapping(key = "id", csvFile = "lotteryUseCondition.csv")
lotteryFee - @TestMapping(key = …
Run Code Online (Sandbox Code Playgroud)

java reflection annotations

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

使用反射循环IEnumerable <>中的值

给定一个可能包含一个对象的对象IEnumerable<T>,我如何检查一个IEnumerable<T>属性是否存在,如果存在,则IEnumerable<T>使用反射遍历所有值,对于任何T?

c# generics reflection

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

通过Go中的反射快速检测空值

我有一个int/ string/ bool/ etc ..值存储在一个interface{}并想确定它是否未初始化,这意味着它的值为

  • 0
  • ""
  • false
  • 要么 nil

我该如何检查?

reflection go

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

Void真的无法实现吗?

在对javadoc中Void说:

Void类是一个不可实例化的占位符类,用于保存对表示Java关键字void的Class对象的引用.

但构造函数很简单:

private Void() {}
Run Code Online (Sandbox Code Playgroud)

这段代码实例化了一个Void:

Constructor<Void> c = Void.class.getDeclaredConstructor();
c.setAccessible(true);
Void v = c.newInstance(); // Hello sailor
Run Code Online (Sandbox Code Playgroud)

所以Void是不是不可实例.

是否有办法让Void真正无法实现?

java reflection void

27
推荐指数
3
解决办法
2689
查看次数

如何将reflect.Value转换为其类型?

如何将reflect.Value转换为其类型?

type Cat struct { 
    Age int
}

cat := reflect.ValueOf(obj)
fmt.Println(cat.Type()) // Cat

fmt.Println(Cat(cat).Age) // doesn't compile
fmt.Println((cat.(Cat)).Age) // same
Run Code Online (Sandbox Code Playgroud)

谢谢!

reflection go

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