I have tried plugging in the code from the supposedly duplicate question several times and could never get it to work. Code from other question/answer:
(T)Activator.CreateInstance(typeof(T), new object[] { weight });
Again, as mentioned below/previously: I already tried that other question's solution and it didn't work for me. Perhaps someone could please try explaining to me how to use the other answer's code in my project rather than not actually reading my post and marking it duplicate?
I am having issues trying to get a Generic Extension method to work. My pseudo-pseudo-code:
internal static class MyExtensions
{
public static void CreateDeck<T>(this ObservableCollection<T> deck)
{
...
Card card = null;
if (deck.GetType() == typeof(FearCard))
{
card = new FearCard(i, front, back);
}
else if (deck.GetType() == typeof(EventCard))
{
card = new EventCard(i, front, back);
}
else if (deck.GetType() == typeof(InvaderCard))
{
card = new InvaderCard(i, front, back);
}
else if (deck.GetType() == typeof(BlightCard))
{
card = new BlightCard(i, front, back);
}
deck.Add(card);
...
}
}
The rest of my code (not included) works fine, but this block needs simplified. I know this is not good code, I just wrote this out so someone could (hopefully) see what I'm actually trying to accomplish and can show me the correct way to implement this. I have tried Activator.CreateInstance
but can't seem to get it to work correctly.
This is the actual code I've tried most recently:
var temp = dType;
Type[] typeArgs = { dType };
var temp2 = temp.MakeGenericType(typeArgs);
var card = Activator.CreateInstance(temp2);
deck.Add(card);
And:
dynamic card = new dynamic(i, front, back);
deck.Add(card);
And:
T card = (T)Activator.CreateInstance(typeof(T), new object[] { i, front, back });
deck.Add(card);
And other versions/attempts of the above.
But none of them work. =(
The "deck" object passed to the method will be a collection of items of one of 4 different types. I need to create objects of the corresponding type and add them to the collection. EX: if the method gets passed a deck of <type A>
then i need to create a card of to put in it.
If this was non generic, the line of code would look like this:
FearCard fc = new FearCard(i, front, back);
The trick is that the item I need to create might not be a FearCard
. It might be a BlightCard
, or several other types. so I just need to replace this line of code with a "generic friendly" version of itself.
Note: the types FearCard
, BlightCard
, etc. are all derived from a public abstract class Card
.