我有一个看起来像这样的课:
public class Field
{
public string FieldName;
public string FieldType;
}
Run Code Online (Sandbox Code Playgroud)
并且List<Field>具有值的对象:
{"EmployeeID","int"},
{"EmployeeName","String"},
{"Designation","String"}
Run Code Online (Sandbox Code Playgroud)
我想创建一个如下所示的类:
Class DynamicClass
{
int EmployeeID,
String EmployeeName,
String Designation
}
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点?
我希望在运行时生成它.我不希望物理CS文件驻留在我的文件系统中.
我正在寻找一种通过重写字节代码并重新加载类来动态添加字段到线程的方法,不确定它是否完全可能.欢迎任何指示.我发现了一些关于修改和加载类的信息,我知道JRebel可以无缝地交换你的代码,但不确定这里是否适用相同的方法/工具.
这里的动机是探索理论上更好的替代线程本地对象.如果方法有效,我应该能够用注释替换本地线程,结果应该优于当前的JDK实现.
PS:请救我"所有邪恶言论的根源"
澄清用例:
想象一下,我有一个ThreadLocal类:
class A {
ThreadLocal<Counter> counter;
...
counter.get().inc()
}
Run Code Online (Sandbox Code Playgroud)
我想用注释替换它:
class A {
@ThreadLocal
Counter counter;
...
counter.inc()
}
Run Code Online (Sandbox Code Playgroud)
但是代替上面的代码生成我想改变Thread,这样Thread现在有一个Acounter字段,实际的代码将是:
class A {
// Nothing here, field is now in Thread
...
Thread.currentThread().Acounter.inc()
}
Run Code Online (Sandbox Code Playgroud) java classloader dynamic-class-loaders dynamic-class-creation
我想在python中在运行时动态创建类.
例如,我想复制下面的代码:
>>> class RefObj(object):
... def __init__(self, ParentClassName):
... print "Created RefObj with ties to %s" % ParentClassName
... class Foo1(object):
... ref_obj = RefObj("Foo1")
... class Foo2(object):
... ref_obj = RefObj("Foo2")
...
Created RefObj with ties to Foo1
Created RefObj with ties to Foo2
>>>
Run Code Online (Sandbox Code Playgroud)
...但我希望动态创建Foo1,Foo2,Foo类(即:在执行期间而不是在第一次通过编译时).
实现这一目标的一种方法是type(),像这样:
>>> class RefObj(object):
... def __init__(self, ParentClassName):
... print "Created RefObj with ties to %s" % ParentClassName
... def make_foo_class(index):
... name = "Foo%s" % index
... return type(name, …Run Code Online (Sandbox Code Playgroud) 我正在尝试将动态类添加到玉器模板中.像这样:
- var obj = {a: 1, b: 2, c: 3};
- var len = Object.keys(obj).length;
.abc-#{len}
Run Code Online (Sandbox Code Playgroud)
但编译器对此不以为然:
> 4| .abc-#{len}
------------^
Unexpected token `interpolation` expected `text`, `interpolated-code`, `code`, `:`, `slash`, `newline` or `eos`
Run Code Online (Sandbox Code Playgroud)
我已经尝试了我能想到的一切.正在搜索https://pugjs.org/language/interpolation.html.真的可以用一只手.
谢谢.
有谁知道如何使用Java实现强大的代码移动性?你以前做过吗?
这是我试图实现的目标.
假设我们有2个通过网络进行通信的独立Java应用程序.App A和App B.
App A将类x实例化为对象,并一直在使用它.App B没有此类x的先验知识.
App A需要将类x 的实例迁移到App B.应用程序B应该能够动态加载类x,并保留类x 的状态.
我已经谷歌搜索了一些关于如何在运行时动态加载类的资源.但是,我不确定是否覆盖了通过网络传输对象实例及其状态并动态调用它的机制.
任何指针都会非常有用,请提前感谢!
注:我最感兴趣的是如何这个问题(即方法,方式思考)解决,而不是什么来解决这个问题; 这是因为我的任务是提出自己的解决方案来解决这个问题.虽然指出图书馆/框架是伟大的,但如果答案是从那些曾经做过类似事情的人那里发布的(尽管很少见),那将是理想的选择.
使用Delphi 2010
SQLQuery1.First; // move to the first record
while(not SQLQuery1.EOF)do begin
// do something with the current record
// What's the code should i write in this part in order to create a TEdit
// containing the user fullname the current item.
ShowMessage(SQLQuery1['whom']);
SQLQuery1.Next; // move to the next record
end;
Run Code Online (Sandbox Code Playgroud) 目前,我在实现接口的类中实现了我的VaryByCustom功能 IOutputCacheVaryByCustom
public interface IOutputCacheVaryByCustom
{
string CacheKey { get; }
HttpContext Context { get; }
}
Run Code Online (Sandbox Code Playgroud)
实现此接口的类有一些约定,类的名称将为"OutputCacheVaryBy_______",其中空白是从页面上的varyByCustom属性传入的值.另一个约定是Context将通过构造函数注入来设置.
目前我的基础是enum和switch语句类似于
public override string GetVaryByCustomString(HttpContext context,
string varyByCustomTypeArg)
{
//for a POST request (postback) force to return back a non cached output
if (context.Request.RequestType.Equals("POST"))
{
return "post" + DateTime.Now.Ticks;
}
var varyByCustomType = EnumerationParser.Parse<VaryByCustomType?>
(varyByCustomTypeArg).GetValueOrDefault();
IOutputCacheVaryByCustom varyByCustom;
switch (varyByCustomType)
{
case VaryByCustomType.IsAuthenticated:
varyByCustom = new OutputCacheVaryByIsAuthenticated(context);
break;
case VaryByCustomType.Roles:
varyByCustom = new OutputCacheVaryByRoles(context);
break;
default:
throw new ArgumentOutOfRangeException("varyByCustomTypeArg");
}
return context.Request.Url.Scheme + …Run Code Online (Sandbox Code Playgroud) 我有一个类,例如:
public class Sample{
private String a;
private String b;
public Sample(String a, String b)
{
this.a = a;
this.b = b;
}
public String getA() {return a;}
public String getB() {return b;}
}
Run Code Online (Sandbox Code Playgroud)
我想创建一个从 Sample 类继承的动态类,并向其中添加字段(字符串字段)。
我试图做:
DynamicType.Builder<? extends Sample> classBuilder = new ByteBuddy()
.subclass(Sample.class, ConstructorStrategy.Default.NO_CONSTRUCTORS)
.name("sampleSon");
classBuilder.defineConstructor(Visibility.PUBLIC)
.withParameters(String.class, String.class, String.class)
.intercept(MethodCall.invoke(Sample.class.getConstructor(String.class, String.class))
.withArgument(0, 1)
.andThen(FieldAccessor.ofField("c").setsArgumentAt(2)));
Run Code Online (Sandbox Code Playgroud)
但是当我尝试从这个类创建一个实例时:
Class<? extends Sample> newSampleClass= classBuilder.make().load(ClassLoader.getSystemClassLoader()).getLoaded();
Sample sample = newSampleClass.getConstructor(String.class, String.class, String.class).newInstance("a", "b", "c");
Run Code Online (Sandbox Code Playgroud)
它抛出一个异常:
public class Sample{
private String a;
private String …Run Code Online (Sandbox Code Playgroud) 我有一个接受表名的存储过程,然后它读取表结构并以字符串中的类定义的形式返回表结构.
例如:
string myString =
"
public class TableName
{
public int Column1 { get; set; }
}
"
Run Code Online (Sandbox Code Playgroud)
是否可以从包含类定义的字符串创建一个Class/Type?例如: -
Type type = GenerateType(myString);
Run Code Online (Sandbox Code Playgroud)
我必须将此类型变量传递给我的另一段代码,所以请帮助我从包含类定义的字符串创建类/类型.
我的班级是这样的:
class X {}
class Y extends X {};
class Z extends X {};
Run Code Online (Sandbox Code Playgroud)
我为每个子类(id + class)都有一个枚举:
enum Type {
Y_TYPE(1, Y.class), Z_TYPE(2, Z.class);
int id;
Class c;
public Type(int id, Class c) { this.id = id; this.c = c; }
public static X createInstance() throws Exception {
return c.newInstance();
}
}
Run Code Online (Sandbox Code Playgroud)
然后我用它们如下:
X parseFromID(int id) {
for (Type v : Type.values()) {
if (v.id == id) {
return v.createInstance();
}
}
}
Run Code Online (Sandbox Code Playgroud)
它工作正常,但我想知道这是一种基于整数id创建数据的Java方法吗?有什么不好的东西应该找吗?
有没有办法强制传入的类是X类型而没有冗长的if-else条件?想想我有大量的子类.
为什么要处理整数ID?
我正在编写某种解析器,所以我需要将我从某处获取的整数id转换为适当的对象.
我需要一种方法来为ex运行java方法.createModule("Login")并作为输出具有:
如果模板是
class Name extends Blah implement Blah {
private createdInt;
private int getCreatedInt() {
return createdInt;
}
}
Run Code Online (Sandbox Code Playgroud)
作为回报,我想获得一个动态创建的类:
class Login extends Blah implement Blah {
private loginInt;
private int getLoginInt() {
return loginInt;
}
}
Run Code Online (Sandbox Code Playgroud)
试图研究groovy做到这一点,但找不到任何有用的东西.
PS它不应该在运行时发生,它更像是一个帮助器只用一个按钮实例化这些模块,而不是键入它们
我写了一个创建字典的方法。我需要将这本字典转换为一个类。
字典示例
Dictionary<string, dynamic> myDictionary = new Dictionary<string, dynamic> {
{ "ID1", 12 },
{ "ID2", "Text2"},
{ "ID3", "Text3" }
};
Run Code Online (Sandbox Code Playgroud)
这是需要创建的类的示例:
public class Foo
{
public int ID1 { get; set; }
public string ID2 { get; set; }
public string ID3 { get; set; }
}
Run Code Online (Sandbox Code Playgroud) java ×5
c# ×4
.net ×2
byte-buddy ×1
class ×1
classloader ×1
delphi ×1
delphi-2010 ×1
dynamic ×1
enums ×1
namedtuple ×1
pug ×1
python ×1
reflection ×1
vcl ×1