如何自动创建实例并动态执行其功能的过程?
谢谢
编辑:需要一个传递参数的选项.谢谢
在普通Java中,可以在运行时以编程方式覆盖类的方法(甚至创建新方法)?
即使我在编译时不知道类,我也希望能够这样做.
我在运行时重写的意思是:
abstract class MyClass{
public void myMethod();
}
class Overrider extends MyClass{
@Override
public void myMethod(){}
}
class Injector{
public static void myMethod(){ // STATIC !!!
// do actual stuff
}
}
// some magic code goes here
Overrider altered = doMagic(
MyClass.class, Overrider.class, Injector.class);
Run Code Online (Sandbox Code Playgroud)
现在,这个调用......
altered.myMethod();
Run Code Online (Sandbox Code Playgroud)
...将调用Injector.myMethod()而不是Overrider.myMethod().
Injector.myMethod()是静态的,因为在执行"magic"之后,它会从不同的类实例(它是Overrider)调用,(因此我们阻止它访问本地字段).
我知道对象属性的字符串名称.我将如何使用字符串获取和设置该属性?
嗨,我正在使用C#在类库中工作,我有一些带有一些属性的类.
我只想知道我是否可以添加一些内容来从getType().GetProperties()中排除一些属性.
我想要的一个例子:
课堂考试
{
Run Code Online (Sandbox Code Playgroud)class Test { public string one { get; set; } public string two {get ; set;} }}
如果我这样做:
static void Main(string [] args)
{
Run Code Online (Sandbox Code Playgroud)static void Main(string[] args) { Test t = new Test(); Type ty = t.GetType(); PropertyInfo[] pinfo = ty.GetProperties(); foreach (PropertyInfo p in pinfo) { Console.WriteLine(p.Name); } }}
我希望输出是这样的:
一
或者只是其中一个属性.
有可能做那样的事吗?我不知道C#中是否有某种修饰符或注释,这使我能够按照自己的意愿行事.
谢谢.
我有一个static class与static private readonly构件的VIA类的设置static constructor.下面是一个简化的例子.
public static class MyClass
{
private static readonly string m_myField;
static MyClass()
{
// logic to determine and set m_myField;
}
public static string MyField
{
get
{
// More logic to validate m_myField and then return it.
}
}
}
Run Code Online (Sandbox Code Playgroud)
由于上面的类是一个静态类,我无法创建它的实例,以便利用传递给FieldInfo.GetValue()调用来检索并稍后设置值m_myField.有没有一种方法我不知道要么使用FieldInfo类来获取和设置静态类的值,还是唯一的选择是重构我被要求进行单元测试的类?
我需要能够得到类似以下的东西才能工作:
Type type = ??? // something decided at runtime with .GetType or typeof;
object[] entityList = context.Resources.OfType<type>().ToList();
Run Code Online (Sandbox Code Playgroud)
这可能吗?如果有任何新内容允许,我可以使用.NET 4.
我按名称获取类,我需要使用相应的数据更新它们,我的问题是如何使用java我想要添加方法一些虚拟数据.我不知道类类型我只是获取类名并使用反射来获取他的数据
我使用此代码来获取类实例和
Class<?> classHandle = Class.forName(className);
Object myObject = classHandle.newInstance();
// iterate through all the methods declared by the class
for (Method method : classHandle.getMethods()) {
// find all the set methods
if (method.getName().matches("set[A-Z].*")
Run Code Online (Sandbox Code Playgroud)
并且知道我找到了我想用数据更新它的set方法的列表我该怎么做.
假设在类名中我得到了人,而且类有setSalary和setFirstName等我怎样才能用反射设置它们?
public class Person {
public void setSalery(double salery) {
this.salery = salery;
}
public void setFirstName(String FirstName) {
this.FirstName = FirstName;
}
}
Run Code Online (Sandbox Code Playgroud) 我不能完全理解何时使用这一个或另一个.他们非常相似,现在和他们一起让我感到困惑.说到反射,我不知道是使用普通的Type还是TypeInfo.
.NET Framework本身增加了更多的混淆assembly.DefinedTypes.它检索到IEnumerable<TypeInfo>我认为它的位置IEnumerable<Type>.此外,还有一种typeInfo.AsType()方法.那是什么意思?他们可以互换吗?
而且,他们的方法非常相似.
似乎TypeInfo就像是经典的更强大的版本Type.我错了吗?
谢谢!
我有大约1000个类,我需要计算它的属性数.我有以下代码:
public static int NumberOfProperties()
{
Type type = typeof(C507);
return type.GetProperties().Count();
}
Run Code Online (Sandbox Code Playgroud)
我可以将其复制并粘贴到每个更改typeof参数的类中,但这看起来有点单调乏味.
反正有没有做一个扩展方法来做到这一点var nop = C507.NumberOfProperties();?
假设我在给定类型(类/接口)中有三个方法:
public void foo(Integer integer);
public void foo(Number number);
public void foo(Object object);
Run Code Online (Sandbox Code Playgroud)
使用MethodHandle或反射,我想找到一个对象的最具体的重载方法,该类型的类型仅在运行时已知.即我想在运行时执行JLS 15.12.
例如,假设我在上述类型的方法中包含以下三种方法:
Object object = getLong(); // runtime type is Long *just an example*
MethodHandles.lookup()
.bind(this, "foo", methodType(Void.class, object.getClass()))
.invoke(object);
Run Code Online (Sandbox Code Playgroud)
然后我在概念上会想要foo(Number number)被选中,但上面会抛出一个异常,因为API只会寻找一个foo(Long)方法,而不是别的.请注意,Long此处的用法仅作为示例.对象的类型可以是实践中的任何东西; String,MyBar,Integer,...等等
MethodHandle API中是否有一些东西能够在运行时自动执行与JLS 15.12之后的编译器相同的分辨率?
reflection ×10
c# ×5
java ×3
.net ×2
accessor ×1
dynamic ×1
function ×1
instance ×1
iqueryable ×1
linq ×1
methodhandle ×1
objective-c ×1
runtime ×1
types ×1