如果对象是泛型类型,我想执行测试.我试过以下但没有成功:
public bool Test()
{
List<int> list = new List<int>();
return list.GetType() == typeof(List<>);
}
Run Code Online (Sandbox Code Playgroud)
我做错了什么,我该如何进行这项测试?
是否可以获得通用参数的类型?
一个例子:
public final class Voodoo {
public static void chill(List<?> aListWithTypeSpiderMan) {
// Here I'd like to get the Class-Object 'SpiderMan'
Class typeOfTheList = ???;
}
public static void main(String... args) {
chill(new ArrayList<SpiderMan>());
}
}
Run Code Online (Sandbox Code Playgroud) 我想知道如何使用python的反射功能将python'type'对象转换为字符串.
例如,我想打印一个对象的类型
print "My type is " + type(someObject) # (which obviously doesn't work like this)
Run Code Online (Sandbox Code Playgroud)
编辑:顺便说一句,谢谢大家,我只是寻找简单的打印类型的控制台输出目的,没有什么花哨的.加比的type(someObject).__name__作品很好:)
我试图通过反射接收字段值.问题是我不知道字段类型,必须在获取值时决定它.
此代码导致此异常:
无法将java.lang.String字段com .... fieldName设置为java.lang.String
Field field = object.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
Class<?> targetType = field.getType();
Object objectValue = targetType.newInstance();
Object value = field.get(objectValue);
Run Code Online (Sandbox Code Playgroud)
我试图施放,但我得到编译错误:
field.get((targetType)objectValue)
Run Code Online (Sandbox Code Playgroud)
要么
targetType objectValue = targetType.newInstance();
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
我需要从.NET 4中使用dynamic关键字声明的对象中获取属性及其值的字典?似乎使用反射这是行不通的.
例:
dynamic s = new ExpandoObject();
s.Path = "/Home";
s.Name = "Home";
// How do I enumerate the Path and Name properties and get their values?
IDictionary<string, object> propertyValues = ???
Run Code Online (Sandbox Code Playgroud) 编辑:
当然,我的真实代码看起来并不完全像这样.我试着编写半伪代码,以便更清楚地表达我想做的事情.
看起来它只是把事情搞砸了.
所以,我真正想做的是:
Method<Interface1>();
Method<Interface2>();
Method<Interface3>();
...
Run Code Online (Sandbox Code Playgroud)
嗯......我想也许我可以用反射把它变成一个循环.所以问题是:我该怎么做.我有很反射的浅识.所以代码示例会很棒.
场景如下所示:
public void Method<T>() where T : class
{}
public void AnotherMethod()
{
Assembly assembly = Assembly.GetExecutingAssembly();
var interfaces = from i in assembly.GetTypes()
where i.Namespace == "MyNamespace.Interface" // only interfaces stored here
select i;
foreach(var i in interfaces)
{
Method<i>(); // Get compile error here!
}
Run Code Online (Sandbox Code Playgroud)
原帖:
嗨!
我正在尝试遍历命名空间中的所有接口,并将它们作为参数发送到这样的泛型方法:
public void Method<T>() where T : class
{}
public void AnotherMethod()
{
Assembly assembly = Assembly.GetExecutingAssembly();
var interfaces = …Run Code Online (Sandbox Code Playgroud) 我需要获取具有特定注释的字段的值,因此使用反射我可以获得此字段对象.问题是这个字段将永远是私有的,虽然我事先知道它总是有一个getter方法.我知道我可以使用setAccesible(true)并获取其值(当没有PermissionManager时),但我更喜欢调用其getter方法.
我知道我可以通过查找"get + fieldName"找到该方法(虽然我知道例如布尔字段有时被命名为"is + fieldName").
我想知道是否有更好的方法来调用这个getter(许多框架使用getters/setters来访问属性,所以也许他们以另一种方式做).
谢谢
正如您在下面的代码中看到的,我已将Action<>对象声明为变量.
有人请让我知道为什么这个动作方法委托表现得像一个静态方法?
为什么它会true在以下代码中返回?
public static void Main(string[] args)
{
Action<string> actionMethod = s => { Console.WriteLine("My Name is " + s); };
Console.WriteLine(actionMethod.Method.IsStatic);
Console.Read();
}
Run Code Online (Sandbox Code Playgroud)
