Sorry to revive an old thread, but since this is the post I found when searching for an answer, I figured I should ask here so it's all in one place in case it ends up being helpful. None of the previous posts seem to help with my issue, or maybe I'm not completely understanding the context of the answers.
In short, I want plain and abstract C# classes (some of which may have constructors) that can use [Inject]. It can be seen in "CharacterState" in my example below, which I am creating in Character (which implements ICharacter). Based on other replies, I guess StrangeIoC isn't aware of CharacterState in order to properly use [Inject], but I'm not sure how to do that, and I don't want to be required to use interfaces for the State classes (the last 3 classes).
(Also I know the code can be improved in structure and also minor things like hardcoded values, but I wanted to keep it relatively simple. I'm not asking about the structure, just how to simply inject into plain classes.)
public interface IStats //an interface (singleton) we want to inject in various places
{
void InitStats();
void UpdateEvasionStats(string isStanding);
}
public interface ICharacter
{
void UserInput(int input);
}
public class Character : ICharacter
{
[Inject]
public IStats Stats { get; set; } //not null (working as expected)
private CharacterState state = new StandingState(); //injectionBinder.GetInstance can't be used here since there is no injectionBinder reference
public void UserInput(int input) {
var newState = state.HandleInput(input);
if (newState != null) {
state = newState;
}
}
}
public abstract class CharacterState
{
[Inject]
public IStats Stats { get; set; } //null (this is the problem)
public abstract CharacterState HandleInput(int input);
}
public class StandingState : CharacterState
{
public override CharacterState HandleInput(int input)
{
if (input == 2) {
Stats.UpdateEvasionStats("crouch"); //this does not work
return new CrouchingState(true);
}
return null;
}
}
public class CrouchingState : CharacterState
{
private bool wasStanding { get; set; }
public CrouchingState(bool wasStanding) { //want to use a constructor
this.wasStanding = wasStanding;
}
public override CharacterState HandleInput(int input)
{
if (wasStanding) {
//some logic
}
if (input == 1) {
Stats.UpdateEvasionStats("stand"); //this does not work
return new StandingState();
}
return null;
}
}
Other responses say to use injectionBinder.GetInstance, but I don't have a reference to injectionBinder in any of the above classes, which is where I would want to replace the "new StandingState()". Even then, how would I handle that for objects with constructors?
There was also a mention of using "injectionBinder.injector.Inject(myObject);" which sounds promising, but I don't even know where that would go, and what it would mean to inject a single instance of an object. If I had a reference to injectionBinder wherever I create "new CrouchingState(true);" would that allow me to use that "new" statement with the contructor, and still allow for the binding to happen, almost like a two step injectionBinder.GetInstance()? If so that would be great, but again I'm not aware of a way for "public class Character : ICharacter" to reference injectionBinder even though it can use [Inject].
Am I right to be chasing a way to get a reference to injectionBinder in my "Character" class? If so, how would I do that, or otherwise how can I get the State classes like CrouchingState to have the [Inject] attribute to actually get a reference to the singleton as the Character class can?
Any help would be greatly appreciated, I feel completely stuck on this one.