这是关于使用反射转换值的问题的后续行动.将某种类型的对象转换为另一种类型可以这样做:
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) 请注意以下简单的源代码:
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) 我需要由字符串名称定义的对象(或"单例对象"或"伴随对象"......除了类之外的任何东西).换句话说,如果我有:
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) 正如标题所说,如何使用反射检查类定义是否定义为内部?"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#中列出类的所有变量(字段).如果是,可能有人给我一些例子如何将它们保存在一个List并获得它们可能使用Anonymous Types(var).
我是一名新的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) 给定一个可能包含一个对象的对象IEnumerable<T>,我如何检查一个IEnumerable<T>属性是否存在,如果存在,则IEnumerable<T>使用反射遍历所有值,对于任何T?
我有一个int/ string/ bool/ etc ..值存储在一个interface{}并想确定它是否未初始化,这意味着它的值为
0""falsenil我该如何检查?
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真正无法实现?
如何将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 ×10
c# ×5
go ×2
java ×2
.net ×1
annotations ×1
generics ×1
scala ×1
singleton ×1
void ×1