I am currently making a program that processes some annotations in java and then builds an alloy model, parses it using the alloy api and then runs some alloy commands. When I test the generated alloy model in the alloy app it works fine and gives me the expected results. However, when I run the generated alloy model through the API, it tells me: "You must specify a scope for sig this/ObjectName".
I read the alloy code from a string like this.
world = CompUtil.parseOneModule(String model);
The only options I use are the SAT4J solver and a skolemdepth of 1.
I then iterate over the commands from world, translates them to kodkod, and executes them.
for(Command command: world.getAllCommands()) {
A4Solution ans = null;
try {
ans = TranslateAlloyToKodkod.execute_command(rep, world.getAllReachableSigs(), command, options);
} catch (Err ex) {
Logger.getLogger(AlloyTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
My UPDATED alloy code looks like this:
module mvc
// General model
abstract sig Configuration { elements: set Element }
abstract sig Element { references: set Element }
// MVC Style
abstract sig Model extends Element { }
abstract sig View extends Element { }
abstract sig Controller extends Element { }
pred mvc_model_style [c: Configuration] {
all m: c.elements & Model | all r: m.references | r not in View
}
pred mvc_view_style [c: Configuration] {
all view: c.elements & View | all ref: view.references | ref not in Model
}
pred mvc_controller_style [c: Configuration] {
all controller: c.elements & Controller | all ref: controller.references | ref in Model or ref in View or ref in Controller
}
pred mvc_style [c: Configuration]{
mvc_model_style[c] mvc_view_style[c]
}
one sig testMvc extends Configuration { } {
elements = TestController + ViewTest + TestModel + TestController3
}
one sig TestController extends Controller { } {
references = TestController + TestModel
}
one sig ViewTest extends View { } {
references = TestController
}
one sig TestModel extends Model { } {
references = ViewTest + TestController3
}
one sig TestController3 extends Controller { } {
references = TestController + TestModel
}
assert testcontroller {
mvc_controller_style[testMvc]
}
assert viewtest {
mvc_view_style[testMvc]
}
assert testmodel {
mvc_model_style[testMvc]
}
assert testcontroller3 {
mvc_controller_style[testMvc]
}
check testcontroller for 8 but 1 Configuration
check viewtest for 8 but 1 Configuration
check testmodel for 8 but 1 Configuration
check testcontroller3 for 8 but 1 Configuration
So does anybody have any idea for how I can fix this, as I thought that the for 1 Configuration, 8 Elements would set the scope for all the extending sigs?
Edit*
I updated my alloy model with the suggestions and I stil get the same error: "You must specify a scope for sig "this/Controller"
The above alloy code works in the Alloy Analyzer and gives this result:
Executing "Check testcontroller for 8 but 1 Configuration"
Solver=sat4j Bitwidth=0 MaxSeq=0 SkolemDepth=1 Symmetry=20
83 vars. 26 primary vars. 98 clauses. 5ms.
No counterexample found. Assertion may be valid. 1ms.
Executing "Check viewtest for 8 but 1 Configuration"
Solver=sat4j Bitwidth=0 MaxSeq=0 SkolemDepth=1 Symmetry=20
65 vars. 25 primary vars. 75 clauses. 5ms.
No counterexample found. Assertion may be valid. 0ms.
Executing "Check testmodel for 8 but 1 Configuration"
Solver=sat4j Bitwidth=0 MaxSeq=0 SkolemDepth=1 Symmetry=20
65 vars. 25 primary vars. 75 clauses. 5ms.
found. Assertion is invalid. 6ms.
Executing "Check testcontroller3 for 8 but 1 Configuration"
Solver=sat4j Bitwidth=0 MaxSeq=0 SkolemDepth=1 Symmetry=20
83 vars. 26 primary vars. 98 clauses. 6ms.
No counterexample found. Assertion may be valid. 0ms.
Your Alloy model contains syntax errors, so you couldn't run it using the Alloy Analyzer either.
First of all, the correct way to specify scope for your testcontroller check is this
check testcontroller for 8 but 1 Configuration
This means "for 8 atoms of everything, but 1 atom of Configuration", whereas what you wrote doesn't event parse.
Next, the mvc_controller_style predicate is undefined, which will also cause you problems.
As for your API usage, just change parseOneModule to parseEverything_fromFile and it should work. I would also expect parseOneModule to work in this case (because there is only one module in your model), but it just doesn't, because, for some reason, some names don't get properly resolved. I'm not sure whether that's a bug or maybe that method is not supposed to be part of the public API. Anyway, here is my code that worked properly for your example:
public static void main(String[] args) throws Exception {
A4Reporter rep = new A4Reporter();
Module world = CompUtil.parseEverything_fromFile(rep, null, "mvc.als");
A4Options options = new A4Options();
options.solver = A4Options.SatSolver.SAT4J;
options.skolemDepth = 1;
for (Command command : world.getAllCommands()) {
A4Solution ans = null;
try {
ans = TranslateAlloyToKodkod.execute_commandFromBook(rep, world.getAllReachableSigs(), command, options);
System.out.println(ans);
} catch (Err ex) {
Logger.getLogger(AlloyTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Related
I try to understand more about how Geb and Spock work internally to understand what is really happening in my tests.
I found that GebSpec which I extend to write my tests has a field Browser _browser.
I also found that GebSpec has a method getBrowser() which returns _browser, so _browser can be accessed over getBrowser() and get_browser(). But the interesting part is that while debugging in intelliJ expanding an instance of GebSpec shows no field _browser but only a field $spock_sharedField__browser.
A little example:
Debugging my Class: The instance of GebSpec has a field spock_sharedField__browser but no field _browser
How do they manage to hide the _browser field from me in the debugger and why do they do it?
Recall a field Browser _browser is declared in GebSpec and a field $spock_sharedField__browser is not.
There also is no method get$spock_sharedField__browser() but I still can access and manipulate $spock_sharedField__browser.
I tried to do it myself:
I wrote a class TestClass that declares _browser exactly analog to GebSpec, but if I debug here the field _browser is shown normally as one would expect
Can someone explain me what is going on?
Why hide _browser?
What is $spock_sharedField__browser good for?
UPDATE: I think the following code describes summarizes it pretty good:
import geb.spock.GebSpec
class GebHomeSpec extends GebSpec{
def "test Geb homepage"(){
when:
['get$spock_sharedField__browser', 'getBrowser', 'get_browser'].each {
try {
println this."${it}"()
} catch (MissingFieldException e) {
println e
}
}
['$spock_sharedField__browser', 'browser', '_browser'].each {
try {
println this.getMetaClass().getAttribute(this, it)
} catch (MissingFieldException e){
println e
}
}
then:
true
}
}
The result on the console is:
null
geb.Browser#352ff4da
geb.Browser#352ff4da
null
groovy.lang.MissingFieldException: No such field: browser for class: GebHomeSpec
groovy.lang.MissingFieldException: No such field: _browser for class: GebHomeSpec
My interpretation, considering the answer of kriegaex, is that in the Compilation during the Spock transformation the field $spock_sharedField__browser is declared and the field _browser is removed. The field browser never existed. But there are still getters for browser and _browser. I wonder where there get their data from (in this case geb.Browser#352ff4da) as none of the field exists anymore as the exceptions show. At least it matches with the debugging information (c.f. first picture/link) that shows the field $spock_sharedField__browser but neither a field _browser nor a field browser.
Finally I noticed (and I dont really know how to explain that) the getters for _browser and browser are outside of the class no longer available (see below). I thought the concept of private is not implemented in groovy and making getters private makes no sense to me anyways.
import geb.spock.GebSpec
class Main {
static void main(String[] args) {
GebSpec gebSpec = new GebSpec()
['get$spock_sharedField__browser', 'getBrowser', 'get_browser'].each {
try {
println gebSpec."${it}"()
} catch (MissingFieldException e) {
println e
}
}
['$spock_sharedField__browser', 'browser', '_browser'].each {
try {
println gebSpec.getMetaClass().getAttribute(gebSpec, it)
} catch (MissingFieldException e){
println e
}
}
}
}
This leads to
null
groovy.lang.MissingFieldException: No such field: $spock_sharedField__browser for class: org.codehaus.groovy.runtime.NullObject
groovy.lang.MissingFieldException: No such field: $spock_sharedField__browser for class: org.codehaus.groovy.runtime.NullObject
null
groovy.lang.MissingFieldException: No such field: browser for class: geb.spock.GebSpec
groovy.lang.MissingFieldException: No such field: _browser for class: geb.spock.GebSpec
All in all I find this rather confusing and I wonder what this is good for. Why introduce $spock_sharedField__browser and remove _browser?
If you use IntelliJ IDEA, you can just decompile the GebSpec class and will see something like this (this is what the Groovy compiler really produced when it compiled the library class):
public class GebSpec extends Specification implements GroovyObject {
// ...
#Shared
#FieldMetadata(
line = 29,
name = "_browser",
ordinal = 2
)
protected volatile Browser $spock_sharedField__browser;
// ...
public Browser createBrowser() {
CallSite[] var1 = $getCallSiteArray();
return !__$stMC && !BytecodeInterface8.disabledStandardMetaClass() ? (Browser)ScriptBytecodeAdapter.castToType(var1[8].callConstructor(Browser.class, this.createConf()), Browser.class) : (Browser)ScriptBytecodeAdapter.castToType(var1[6].callConstructor(Browser.class, var1[7].callCurrent(this)), Browser.class);
}
public Browser getBrowser() {
CallSite[] var1 = $getCallSiteArray();
if (BytecodeInterface8.isOrigZ() && !__$stMC && !BytecodeInterface8.disabledStandardMetaClass()) {
if (ScriptBytecodeAdapter.compareEqual(var1[11].callGroovyObjectGetProperty(this), (Object)null)) {
Browser var3 = this.createBrowser();
ScriptBytecodeAdapter.setGroovyObjectProperty(var3, GebSpec.class, this, (String)"_browser");
}
} else if (ScriptBytecodeAdapter.compareEqual(var1[9].callGroovyObjectGetProperty(this), (Object)null)) {
Object var2 = var1[10].callCurrent(this);
ScriptBytecodeAdapter.setGroovyObjectProperty((Browser)ScriptBytecodeAdapter.castToType(var2, Browser.class), GebSpec.class, this, (String)"_browser");
}
return (Browser)ScriptBytecodeAdapter.castToType(var1[12].callGroovyObjectGetProperty(this), Browser.class);
}
// ...
public Browser get$spock_sharedField__browser() {
return this.$spock_sharedField__browser;
}
public void set$spock_sharedField__browser(Browser var1) {
this.$spock_sharedField__browser = var1;
}
}
I think you have dived deep enough already to understand without further explanation.
Update: I forgot to mention: Your test class does not inherit GebSpec (which again inherits from Specification, i.e. the code will not be transformed by Spock/Geb because it has the wrong base class. If you do this, though:
package de.scrum_master.stackoverflow
import geb.spock.GebSpec
import spock.lang.Shared
class FooIT extends GebSpec {
#Shared
def myField
def test() {
expect:
true
}
}
Then the decompiled code will be:
package de.scrum_master.stackoverflow;
import geb.spock.GebSpec;
import org.codehaus.groovy.runtime.ScriptBytecodeAdapter;
import org.codehaus.groovy.runtime.callsite.CallSite;
import org.spockframework.runtime.ErrorCollector;
import org.spockframework.runtime.SpockRuntime;
import org.spockframework.runtime.ValueRecorder;
import org.spockframework.runtime.model.BlockKind;
import org.spockframework.runtime.model.BlockMetadata;
import org.spockframework.runtime.model.FeatureMetadata;
import org.spockframework.runtime.model.FieldMetadata;
import org.spockframework.runtime.model.SpecMetadata;
import spock.lang.Shared;
#SpecMetadata(
filename = "FooIT.groovy",
line = 6
)
public class FooIT extends GebSpec {
#Shared
#FieldMetadata(
line = 7,
name = "myField",
ordinal = 0
)
protected volatile Object $spock_sharedField_myField;
public FooIT() {
CallSite[] var1 = $getCallSiteArray();
}
#FeatureMetadata(
line = 10,
name = "test",
ordinal = 0,
blocks = {#BlockMetadata(
kind = BlockKind.EXPECT,
texts = {}
)},
parameterNames = {}
)
public void $spock_feature_1_0() {
CallSite[] var1 = $getCallSiteArray();
ErrorCollector $spock_errorCollector = (ErrorCollector)ScriptBytecodeAdapter.castToType(var1[2].callConstructor(ErrorCollector.class, false), ErrorCollector.class);
ValueRecorder $spock_valueRecorder = (ValueRecorder)ScriptBytecodeAdapter.castToType(var1[3].callConstructor(ValueRecorder.class), ValueRecorder.class);
Object var10000;
try {
try {
SpockRuntime.verifyCondition($spock_errorCollector, $spock_valueRecorder.reset(), "true", Integer.valueOf(12), Integer.valueOf(5), (Object)null, $spock_valueRecorder.record($spock_valueRecorder.startRecordingValue(Integer.valueOf(0)), true));
var10000 = null;
} catch (Throwable var13) {
SpockRuntime.conditionFailedWithException($spock_errorCollector, $spock_valueRecorder, "true", Integer.valueOf(12), Integer.valueOf(5), (Object)null, var13);
var10000 = null;
} finally {
;
}
var1[4].call(var1[5].call(this.getSpecificationContext()));
} finally {
$spock_errorCollector.validateCollectedErrors();
var10000 = null;
}
}
public Object get$spock_sharedField_myField() {
return this.$spock_sharedField_myField;
}
public void set$spock_sharedField_myField(Object var1) {
this.$spock_sharedField_myField = var1;
}
}
Update 2:
As for your additional questions, I can only speculate about the answers, I am sure users like #erdi (Geb maintainer), #Szymon Stepniak, #Leonard Brünings (who seem to be Groovy cracks, which I am not) could say more about it, but OTOH this is not a discussion forum and the questions are not particularly well suited for SO. Anyway, I edited the question tags to include "groovy" so as to maybe raise their attention.
Why introduce $spock_sharedField__browser and remove _browser?
I think it is just the result of Spock's way of transforming the #Shared annotation into a member variable with named so as to very unlikely collide with any existing member names. You also see this happening in the decompiled version of my own Spock/Geb specification.
But there are still getters for browser and _browser.
Of course there is a getter for browser, as in the Geb DLS you usually don't look behind the scenes but just use the syntactic sugar browser to access the browser instance. This Groovy-ism will call getBrowser(), as you probably know. This particular getter is declared explicitly in the GebSpec class in order to make the member conveniently accessible (you also see some lazy browser instantiation logic here):
Browser getBrowser() {
if (_browser == null) {
_browser = createBrowser()
}
_browser
}
I wonder where there get their data from (in this case geb.Browser#352ff4da) as none of the field exists anymore as the exceptions show.
I do not know enough about Groovy's dynamic language features to answer that, but you can see the actual mechanics in my decompiled code snippets.
Accessing Spock-specific class members from outside a running specification obviously does not work and probably is not meant to be. But if you run this test, it works just fine:
package de.scrum_master.stackoverflow
import geb.spock.GebSpec
import spock.lang.Shared
class FooIT extends GebSpec {
#Shared
def myField = "foo"
def test() {
given:
println browser
println myField
expect:
true
}
}
Console log:
geb.Browser#1722011b
foo
I have a test with a data table, e.g. like this:
#Unroll
"Basic session start on platform = #platform"() {
when: "user stats a session on a platform"
def response = startSession(platform, id) // it does a REST request
then: "response contains a userId"
assert verifyUserId(response.userId) // method verifies userId with an internal algorithm and returns true\false if it is valid or not
where:
id | platform
1 | "Facebook"
2 | "Google"
3 | "Apple"
}
I have also wrote a listener for errors.
class MyListener extends AbstractListener {
...
public void error(ErrorInfo error) { ... }
...
}
So that in case an assertion error happens during test execution, the code goes into this "error" method.
The question is, how can I get values of variables from the "where" block from inside the "error" method?
I don't do direct assertions for the data table variables, so ErrorInfo.Exception does not contain them.
I also could not find any other suitable members of the "ErrorInfo" object - I can only find variable names in ErrorInfo.getMethod().getFeature(), but not their values that were there when the error happened.
The main issue here is that ErrorInfo doesn't provide current spec instance, that's why you can't get iteration variables. So I would suggest two ways:
First one is to use iteration interceptor:
class ListenForErrorsExtension implements IGlobalExtension {
void visitSpec(SpecInfo specInfo) {
specInfo.addListener(new Listener())
specInfo.allFeatures*.addIterationInterceptor(new IMethodInterceptor() {
#Override
void intercept(IMethodInvocation invocation) {
holder.put(invocation.iteration.dataValues)
invocation.proceed()
}
})
}
The second one was kindly suggested in comments by Opal. You could instantinate not AbstractRunListener, but IRunListener
class ErrorExtension implements IGlobalExtension{
....
#Override
void beforeIteration(IterationInfo iteration) {
holder.put(iteration.dataValues)
}
....
}
It gives you access to each iterration, but the main idea is still following:
You have to create thread safe holder, put iterration data to it BEFORE error happened and then extract.
I am getting weird results from a Spock unit test that I thought was being caused by a misuse of Groovy's TupleConstructor annotation. However, thanks to the help of another user, I see it is a problem with the way Spock is creating mocks. Although I have fixed the issue by replacing the injected mocks with real instances, I need to in fact get mocks working here.
My main classes:
#Canonical
#TupleConstructor(callSuper = true)
abstract class Vehicle {
Long id
}
#Canonical
#TupleConstructor(callSuper = true, includeSuperProperties = true)
abstract class Foobaz extends Vehicle {
String name
String label
String description
}
#Canonical
#TupleConstructor(callSuper = true, includeSuperProperties = true)
class Fizz extends Foobaz {
// This is an empty class that creates a meaningful name over the
// abstract Foobaz parent class. This may seem like bad design in
// this analogy, but I assure you it makes sense (from a Domain-Driven
// Design perspective) in my actual application.
}
#Canonical
#TupleConstructor(callSuper = true, includeSuperProperties = true)
class Car extends Vehicle {
Fizz fizz1
Fizz fizz2
#Override
String toString() {
"${fizz1.name} - ${fizz2.name}"
}
}
My Spock test:
class CarSpec extends Specification {
def "toString() generates a correct string"() {
given: "a Car with some mocked dependencies"
String f1 = 'fizzy'
String f2 = 'buzzy'
Fizz fizz1 = Mock(Fizz)
Fizz fizz2 = Mock(Fizz)
fizz1.name >> f1
fizz2.name >> f2
Car car = new Car(1L, fizz1, fizz2)
when: "we call toString()"
String str = car.toString()
then: "we get a correctly formatted string"
"${f1} - ${f2}" == str
}
}
But when I run this I get the following failure/error:
Condition not satisfied:
"${f1} - ${f2}" == str
| | | |
fizzy buzzy | null - null
false
<omitting details here for brevity>
Expected :null - null
Actual :fizzy - buzzy
Any ideas where I'm going awry?
If you change your specification to this:
class CarSpec extends Specification {
def "toString() generates a correct string"() {
given: "a Car with some mocked dependencies"
String f1 = 'fizzy'
String f2 = 'buzzy'
Fizz fizz1 = Mock()
Fizz fizz2 = Mock()
Car car = new Car(1L, fizz1, fizz2)
when: "we call toString()"
String str = car.toString()
then: "we get a correctly formatted string + getProperty('name') is called once on each Mock"
"$f1 - $f2" == str
1 * fizz1.getProperty('name') >> f1
1 * fizz2.getProperty('name') >> f2
}
}
So you define the interactions in the then block, then it should all work fine...
From our discussion on a different one of #smeeb's questions, I looked into this a bit more since I was very confused why this wasn't working.
I created my own test.
class SomeTest extends Specification {
static class Driver {
String getName(Superclass superclass) {
return superclass.name
}
}
static abstract class Superclass {
String name
}
static class Subclass extends Superclass {
}
def 'test'() {
given:
def driver = new Driver()
def subclass = Mock(Subclass)
subclass.name >> 'test'
expect:
driver.getName(subclass) == 'test'
}
}
It failed with the same problem that #smeeb saw.
driver.getName(subclass) == 'test'
| | | |
| null | false
| Mock for type 'Subclass' named 'subclass'
I tried changing a few different things and found that when I either removed the abstract modifier from Superclass or changed the return superclass.name to return superclass.getName() the test began working.
It seems there is a weird interaction on the Groovy-level between getting inherited public fields from an abstract superclass using the auto-generated accessors.
So, in your case either remove abstract modifier from FooBaz, or change your code to:
#Override
String toString() {
"${fizz1.getName()} - ${fizz2.getName()}"
}
I often need to use some class which itself have to load some dependency to work.
However, my component can have more than one concrete dependency implementation and it will choose one, rather than another one on some object parameter basis.
The real problem is that the object parameter is always unknown when application start up, so I'm not able in this moment to register any dependency, neither to resolve them.
Instead, for instance, when I need to use some class which itself needs to load some dependency I know the object parameter used by concreteBuilder in order to return me the appropriate implementation:
interface ISample { }
class ParamForBuildSomeISampleImplementation
{
// this instance cannot be create by my startUpApplication - Container - Resolver.
// Instead, all time dependency is required (buttonClick, pageLoad and so on), this class can be instantiated.
}
class Sample1 : ISample
{
// some implementation
}
class Sample2 : ISample
{
// some other implementation
}
class MyISampleFactory
{
// Build ISample
public ISample Build(ParamForBuilderISample obj)
{
// if obj.someProperty == ".." return new Sample1();
// else if obj.someProperty == "--" return new Sample2();
// else if ...
}
}
class NeedsDependency
{
ISample _someSample;
public NeedsDependency(ISample someSample)
{
_someSample = someSample;
}
}
// *** Controllor - ApplicationStartup - other ***
// Here I have not idea how to build ISample dependency
## EDIT
// *** button click event handler ***
// Ok, here I know how to create ParamForBuilderISample,
// hence I can call MyISampleFactory, then, I can Use NeedDependency class:
ParamForBuilderISample obj = new ...
obj.SomeProperty = ...
obj.otherSomeProperty = ...
ISample sample = MyISampleFactory.Build(obj);
NeedDependency nd = new NeedDependency(sample);
// perfect, now my buttonClick can execute all what it wants
nd.DoSomething();
nd.DoOtherStuff();
Is my scenario suitable to Dependency Injection pattern? If true, I really have not idea how build my pattern.
Instead of using constructor injection for passing in this 'runtime dependency', you might be better of using method injection. This might even completely remove the need for having a factory:
private readonly ISample sample;
public MyController(ISample sample) {
this.sample = sample;
}
public string button_click_event_handler(object s, EventArgs e) {
ParamForBuilderISample obj = new ...
obj.SomeProperty = ...
obj.otherSomeProperty = ...
this.sample.DoSomething(obj);
}
You still need to switch somewhere, but instead of having a factory, you could implement a proxy for ISample:
public class SampleProxy : ISample
{
private readonly Sample1 sample1;
private readonly Sample2 sample2;
public SampleProxy(Sample1 sample1, Sample2 sample2) {
this.sample1 = sample1;
this.sample2 = sample2;
}
public void DoSomething(ParamForBuilderISample param) {
this.GetSampleFor(param).DoSomething(param);
}
private ISample GetSampleFor(ParamForBuilderISample param) {
// if obj.someProperty == ".." return this.sample1;
// else if obj.someProperty == "--" return this.sample2;
// else if ...
}
}
Your ParamForBuilderISample looks like a parameter object. Dependency injection doesn't remove the need to have method arguments. Data should still be passed on through methods.
This question may appear to have been answered before but I have been unable to find exactly what I need. Here is my situation:
// Base class
interface IAnimal {};
public abstract class Animal : IAnimal{}
// Derived classes
interface IDog {}
public class Dog : Animal, IDog { }
interface ICat { }
public class Cat : Animal, ICat { }
interface ITiger { }
public class Tiger : Animal, ITiger { }
interface ILion { }
public class Lion : Animal, ILion { }
// Collection Classes
interface IPets { }
public class Pets
{
IDog dog = new Dog();
ICat cat = new Cat();
}
interface ICircus { }
public class Circus
{
ITiger tiger = new Tiger();
ILion lion = new Lion();
}
I would like to create the collections at run time in an generic Event class by reading in a list animals from xml that would make up the collection. What would be the correct way to accomplish this?
Thanks in advance.
This is kind of an answer to my own question. Maybe this will help others.
I chose a very generic example to illustrate my situation because I have uses for this in many places in Windows Forms, XNA and Silverlight that are all very different.
When I used the Activator, I found out that it assumes the executing assembly. My method is in a library so I had to load a different assembly. Next I had to make sure that I had the right namespace. My base class is in a library and the derived classes are in another namespace so this will require refactoring to properly create the list.
Another problem I found was that the Activator assumes a constructor with no parameters. In my test case all my derived classes are XNA game components with a parameter of type Game.
Have to do some refactoring to test out the interfaces and how the game objects are to interact.
Will be back to this list when I have something further.
Does this sort of example help? (It's from some of my code I happened to have handy.) The key point here is the use of reflection in Activator.CreateInstance(...).
public static List<dynamic> LoadChildEntities(XElement entityElt)
{
var children = new List<dynamic>();
foreach(XElement childElt in entityElt.Elements("entity"))
{
// Look up the C# type of the child entity.
string childTypename = "MyNamespace." + Convert.ToString(childElt.Attribute("type").Value);
Type childType = Type.GetType(childTypename);
if(childType != null)
{
// Construct the child entity and add it to the list.
children.Add(Activator.CreateInstance(childType, childElt));
}
else
{
throw new InvalidOperationException("No such class: " + childTypename);
}
}
return children;
}
If you want a list of IAnimal instead, it wouldn't be too tricky to change.