Questions tagged [dynamic-class-creation]

Some Object Oriented programming languages allow creating new classes (types) in run-time (as compared to classes defined in the source code and created in compilation time)

Some examples of creating types in run-time:
C#
C++
Java
Python

78 questions
262
votes
16 answers

How to dynamically create a class?

I have a class which looks like this: public class Field { public string FieldName; public string FieldType; } And an object List with values: {"EmployeeID","int"}, {"EmployeeName","String"}, {"Designation","String"} I want to…
ashwnacharya
  • 14,601
  • 23
  • 89
  • 112
16
votes
8 answers

How can I modify a java.lang class on the fly?

I'm looking for a way to add fields to an Thread on the fly by rewriting the byte code and reloading the class, not sure if it is at all possible. Any pointers welcome. I found some info on modifying and loading a class, and I know JRebel can…
14
votes
2 answers

Creating Dynamic Classes in Jade/Pug

I'm trying to add a dynamic class to a jade template. Like so: - var obj = {a: 1, b: 2, c: 3}; - var len = Object.keys(obj).length; .abc-#{len} But the compiler is taking exception to this: > 4| .abc-#{len} ------------^ Unexpected token…
NotoriousWebmaster
  • 3,298
  • 7
  • 37
  • 49
9
votes
3 answers

What is the advantage in using `exec` over `type()` when creating classes at runtime?

I want to dynamically create classes at runtime in python. For example, I want to replicate the code below: >>> class RefObj(object): ... def __init__(self, ParentClassName): ... print "Created RefObj with ties to %s" %…
Russ
  • 10,835
  • 12
  • 42
  • 57
6
votes
4 answers

how to dynamically create a component in delphi such as TLabel or TEdit ...etc

Using 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…
Rafik Bari
  • 4,867
  • 18
  • 73
  • 123
6
votes
6 answers

Java: Strong Code mobility How to?

Does anyone know how to use Java for Strong code mobility? Have you ever done it before? Here's what I try to achieve. Suppose we have 2 separate Java applications that communicate over network. App A and App B. App A has a class x instantiated as…
sivabudh
  • 31,807
  • 63
  • 162
  • 228
5
votes
1 answer

Byte Buddy - define constructor with call to super class and initialize field

I have a class such as: 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;} } I want to create a…
Rotem ben
  • 156
  • 2
  • 13
5
votes
6 answers

Best way to dynamically create classes instead of using a switch block

Currently I have my VaryByCustom functionality implemented in classes that implement an interface IOutputCacheVaryByCustom public interface IOutputCacheVaryByCustom { string CacheKey { get; } HttpContext Context { get; } } A class…
Chris Marisic
  • 32,487
  • 24
  • 164
  • 258
4
votes
3 answers

java generics vs dynamically loading class using Class.forName()

Assume I am making a class called Government. Government has members like officers, ministers, departments etc. For each of those members I create an interface, and any specific government defines them as they like. The main method in the Government…
Aman
  • 639
  • 1
  • 9
  • 25
4
votes
2 answers

Reflection to avoid class load

I was reading through the PerfMark code and saw a comment about avoid an accidental class load through using reflection in a commit: if (Boolean.getBoolean("io.perfmark.PerfMark.debug")) { - …
Jane
  • 83
  • 6
4
votes
1 answer

How is the __class__ cell value set in class methods?

Looking at the documentation of the super type in Python 3.5, it notes that super(…) is the same as super(__class__, «first argument to function»). To my surprise, I wrote a method that returned __class__ – and it actually worked: >>> class c: ... …
4
votes
3 answers

Java way to create an object based on enum type

My class is like this: class X {} class Y extends X {}; class Z extends X {}; I have an enum for each subclass (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 =…
w00d
  • 5,416
  • 12
  • 53
  • 85
4
votes
3 answers

Python: dynamic class generation: overwrite members

I have a python class hierarchy, that I want to extend at runtime. Furthermore every class in this hierarchy has a static attribute 'dict', that I want to overwrite in every subclass. Simplyfied it looks like this: 'dict' is a protected (public but…
Philip Daubmeier
  • 14,584
  • 5
  • 41
  • 77
3
votes
1 answer

python: name mangling in a dynamically created class

I want to do something like this def make_class(name : str)->type: class Ret: __name__ = name def __init__(self): self.__x = 0 return Ret A = make_class('A') a = A() assert a._A__x == 0 Basically, what I want is to…
3
votes
1 answer

Creating dynamic class from a string containing the class definition

I have a stored procedure which accepts the table name, then it reads the table structure and returns me the table structure in the form of a class definition in a string. E.g.: string myString = " public class TableName { public int…
Ankur Arora
  • 194
  • 3
  • 15
1
2 3 4 5 6