我需要实现这个方法:
object GetFactory(Type type);
Run Code Online (Sandbox Code Playgroud)
此方法需要返回一个Func <T>,其中typeparam'T'是'type'.
所以,我的问题是我不知道如何使用反射在运行时创建Func <?>.Activator.CreateInstance不起作用,因为委托上没有构造函数.
有任何想法吗?
我有一个Class包含某种类型的变量,我需要获取一个包含相应数组类的变量.我能想到的最好的是:
Class arrayOfFooClass = java.lang.reflect.Array.newInstance(fooClass, 0).getClass();
Run Code Online (Sandbox Code Playgroud)
有没有办法在不创建新实例的情况下执行此操作?
我有以下虚拟类结构,我试图找出如何从PeopleList中的类People的每个实例获取属性.我知道如何从People的单个实例中获取属性,但在我的生活中不能知道如何从PeopleList获取它.我确信这真的很简单,但是有人能指出我正确的方向吗?
public class Example
{
public class People
{
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
private int _age;
public int Age
{
get { return _age; }
set { _age = value; }
}
public People()
{
}
public People(string name, int age)
{
this._name = name;
this._age = age;
}
}
public class PeopleList : List<People>
{
public static void DoStuff()
{
PeopleList newList = new …Run Code Online (Sandbox Code Playgroud) 有没有办法从基类实例中找出派生类的名称?
例如:
class A{
....
}
class B extends A{
...
}
class c extends A{
...
}
Run Code Online (Sandbox Code Playgroud)
现在如果一个方法返回一个对象A,我可以找出它是否是类型B或C?
有没有办法在Ruby中获取某个类的所有对象?
澄清:
class Pokemon
end
pikatchu = Pokemon.new
charmander = Pokemon.new
Run Code Online (Sandbox Code Playgroud)
那么,有没有办法可以以某种方式检索引用那两个对象(pikatchu和charmander)?
我实际上想过通过初始化把它全部推到一个类数组中,但这可能会变大,我假设可能有一个原生的Ruby方法.
我有一个方法的MehtodBase,我需要知道该方法是否是特定接口的实现.所以,如果我有以下课程:
class MyClass : IMyInterface
{
public void SomeMethod();
}
Run Code Online (Sandbox Code Playgroud)
实现界面:
interface IMyInterface
{
void SomeMethod();
}
Run Code Online (Sandbox Code Playgroud)
我希望能够在运行时(使用反射)发现某个方法是否实现了IMyInterface.
public interface Address {
public int getNo();
}
public interface User<T extends Address> {
public String getUsername();
public T getAddress();
}
public class AddressImpl implements Address {
private int no;
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
}
public class UserImpl implements User<AddressImpl> {
private String username;
private AddressImpl addressImpl;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public AddressImpl getAddress() { …Run Code Online (Sandbox Code Playgroud) 在我的一个应用程序中,我使用以下内容:
public void calculate (String className)
{
...
Class clazz = Class.forName(className);
...
}
Run Code Online (Sandbox Code Playgroud)
该函数被调用几次/秒.大约有10个可能的类名.虽然我确实意识到这个函数中有一些内部缓存,但我认为这个缓存只能在本机级别上使用.
出于这个原因,我开始怀疑是否应该添加自己的缓存.
private static Map<String,Class> classMap;
public void calculate (String className)
{
...
Class clazz = classMap.get(className);
if (clazz == null)
{
clazz = Class.forName(className);
if (classMap == null) classMap = new HashMap<String, Class>(40);
classMap.put(className, clazz);
}
...
}
Run Code Online (Sandbox Code Playgroud)
这会是性能提升还是真的没有区别?
先感谢您
假设我在一个使用Java 8类型注释的类中有以下成员:
private List<@Email String> emailAddresses;
Run Code Online (Sandbox Code Playgroud)
是否可以@Email使用反射读取在运行时使用的String类型上给出的注释?如果是这样,怎么办呢?
更新:这是注释类型的定义:
@Target(value=ElementType.TYPE_USE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Email {}
Run Code Online (Sandbox Code Playgroud) 我有一个我想用反射调用的私有静态泛型方法,但实际上我想在另一个方法中"捆绑"它.C#7.0支持本地功能,所以这绝对是可能的.
你会说"你为什么不直接叫它?" 但我正在用它来获得以强类型方式使用对象和System.Type的能力,所以我需要动态调用它.如果我将它作为自己的私有静态泛型方法,则此代码已经有效.
private static void HandleResponse(object data, Type asType)
{
var application = typeof(Program);
application
.GetMethod(nameof(UseAs), BindingFlags.Static | BindingFlags.NonPublic)
.MakeGenericMethod(asType)
.Invoke(null, new object[] { data });
}
public static void UseAs<T>(T obj)
{
Console.WriteLine($"Object is now a: {typeof(T)}:");
};
Run Code Online (Sandbox Code Playgroud)
上面的代码有效.如果我通过:
data: new TestObject(),
type: typeof(TestObject)
Run Code Online (Sandbox Code Playgroud)
我实际上在UseAs中有一个TestObject.
所以,我想把这一切都放在一个方法中,如下所示:
private static void HandleResponse(object data, Type asType)
{
void useAs<T>(T obj)
{
Console.WriteLine($"Object is now a: {typeof(T)}:");
};
var application = typeof(Program);
application
.GetMethod(nameof(UseAs), BindingFlags.Static | BindingFlags.NonPublic)
.MakeGenericMethod(asType)
.Invoke(null, new object[] { …Run Code Online (Sandbox Code Playgroud) reflection ×10
java ×5
c# ×4
inheritance ×2
.net ×1
annotations ×1
arrays ×1
class ×1
generics ×1
methods ×1
oop ×1
performance ×1
properties ×1
ruby ×1