I'm writing a Console app that simulates the ancestry of an animal, for a specified number of generations back. It's for any animal, but I'm using bees as my first example.
I want to instantiate an object of type IAnimal, containing an object of IGender, as I do not know beforehand what these two might be. They differ for different animals.
IAnimal interface:
namespace Gensim
{
internal interface IAnimal
{
string Name { get; set; }
IGender Gender { get; set; } //Includes subgender and fertile/unfertile flag.
int Generation { get; set; }
int? FatherIndex { get; set; }
int? MotherIndex { get; set; }
}
}
The animal type used here:
using Gensim.Genders;
namespace Gensim
{
internal class Bee : IAnimal
{
public IGender Gender { get; set; }
public int Generation { get; set; }
public int? FatherIndex { get; set; }
public int? MotherIndex { get; set; }
public string Name { get; set; }
public Bee(string name, IGender gender, int generation, int? fatherIndex, int? motherIndex)
{
Name = name;
Gender = gender;
Generation = generation;
FatherIndex = fatherIndex;
MotherIndex = motherIndex;
}
}
}
IGender interface:
using System;
using System.Collections.Generic;
namespace Gensim
{
public interface IGender
{
public enum MainTypeEnum
{
male,
female
}
public MainTypeEnum MainType { get; set; }
public bool IsFertile { get; set; }
public List<Type> RequiredParents { get; set; }
}
}
The Gender used here:
using System;
using System.Collections.Generic;
using System.Text;
namespace Gensim.Genders
{
class Queen : IGender
{
public IGender.MainTypeEnum MainType { get; set; }
public bool IsFertile { get; set; }
public List<Type> RequiredParents { get; set; }
public Queen()
{
this.MainType = IGender.MainTypeEnum.female;
this.IsFertile = true;
this.RequiredParents = new List<Type>
{
typeof(Queen),
typeof(Drone)
};
}
}
}
In the Program.cs, I get the exception: System.MissingMethodException: 'Constructor on type 'Gensim.Genders.Queen' not found.'
.
.
.
IGender gender = (IGender)Activator.CreateInstance(RequiredGender);
IAnimal newAnimal = (IAnimal)Activator.CreateInstance(RequiredGender,
new object[] {
animal.GetType().Name + (manager.Animals.Count - 1),
gender,
genCount,
null,
null
}
); <--I receive the exception on this line
manager.Animals.Add(newAnimal);
.
.
.
Which I find odd, because an instance of the Queen class has already been instantiated, before it is passed to the CreateInstance method:
Why would I receive an error, stating that a constructor of a class cannot be found, that has already been instantiated?
How do I fix this?
I have looked at similar problems on Stack Overflow but none of the solutions have solved my problem.
Exception details:
System.MissingMethodException
HResult=0x80131513
Message=Constructor on type 'Gensim.Genders.Queen' not found.
Source=System.Private.CoreLib
StackTrace:
at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture)
at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at System.Activator.CreateInstance(Type type, Object[] args)
at Gensim.Program.Main() in C:\Users\divan\source\repos\Gensim\Program.cs:line 37
User contributions licensed under CC BY-SA 3.0