- Home /
C# How to use inherited generic abstract class with specific type.
So I have this abstract class
abstract public class MenuInfoBase<T> : MonoBehaviour
{
/// <summary>
/// The Skin that will be used.
/// </summary>
public GUISkin Skin;
/// <summary>
/// The type of object passed to menu.
/// </summary>
public MenuTypes Type;
/// <summary>
/// The target type that this menu edits.
/// </summary>
public TargetTypes Target;
/// <summary>
/// Always have OnGUI
/// </summary>
abstract public void OnGUI();
/// <summary>
/// Method to be called so we know when Menu is done.
/// </summary>
abstract public bool Invoke(ref T type);
}
Which will see if a menu will be editing a string, a position, or what ever.
Then A class that inherits from the base class that will only ever be used for stings.
public class DescribeObjectMenu : MenuInfoBase<string>
{
public override bool Invoke(ref string type)
{
throw new NotImplementedException();
}
/// <summary>
/// Edits the target 'InfoBase' description using a Text Area.
/// </summary>
public override void OnGUI()
{
throw new NotImplementedException();
}
}
The problem I am having is that unity throws.
"error CS0308: The non-generic type `MenuInfoBase' cannot be used with the type arguments"
I tried to make DescribeObjectMenu generic also. public class DescribeObjectMenu : MenuInfoBase { public override bool Invoke(ref T type) { throw new NotImplementedException(); }
public override void OnGUI()
{
throw new NotImplementedException();
}
}
but the problem is that I have gameObject that assign to T which need to be of type T, but if it is of type T I can't directly assign strings to it. Like
public string FeildString;
override public bool Invoke(ref T targetString)
{
targetString = FeildString;
return true;
}
How would I get this done without hard wiring a string in there.
I reduced your classes to the generic parts and they worded without a problem. Is there something you didn't post?
abstract public class $$anonymous$$enuInfoBase<T> : $$anonymous$$onoBehaviour
{
abstract public bool Invoke(ref T type);
}
public class DescribeObject$$anonymous$$enu : $$anonymous$$enuInfoBase<string>
{
public override bool Invoke(ref string type)
{
throw new NotImplementedException();
}
}