4

I have been using a 2d engine called NEZ. They use a List of class objects called "components" that are attached to an entity. Component is a public class that can be inherited.

Like let's say:

public class Ninja : Component

This is the Code:

/// <summary>
/// Adds a Component to the components list. Returns the Component.
/// </summary>
/// <returns>Scene.</returns>
/// <param name="component">Component.</param>
/// <typeparam name="T">The 1st type parameter.</typeparam>
public T AddComponent<T>(T component) where T : Component
{
    component.Entity = this;
    Components.Add(component);
    component.Initialize();
    return component;
}

/// <summary>
/// Adds a Component to the components list. Returns the Component.
/// </summary>
/// <returns>Scene.</returns>
/// <typeparam name="T">The 1st type parameter.</typeparam>
public T AddComponent<T>() where T : Component, new()
{
    var component = new T();
    component.Entity = this;
    Components.Add(component);
    component.Initialize();
    return component;
}

So the thing that is annoying is that Visual Studio doesn't give me a list of class objects that have the Component in them.

So, how would I know that _animator = Entity.AddComponent<SpriteAnimator>(); is a Component?

Besides that obvious answer that I would have to look at each .CS file and see if they inherit the Component class.

The question is: The object in <T> doesn't auto complete or doesn't even give me a possible items to fill the blank.

Is there a way for IntelliSense to search for each object that inherits the class Component?

If not, do you know if other IDE or intelli-tool (like Resharper) does the job?

I'm using Visual Studio 2019 with Roslynator Refactorings addon and also using Visual Assistance from Tomatoes Software.

GSerg
  • 76,472
  • 17
  • 159
  • 346
  • Object Browser and look for descendants of Component? Also, https://stackoverflow.com/questions/282377/visual-studio-how-do-i-show-all-classes-inherited-from-a-base-class has some useful stuff – Caius Jard Jun 19 '21 at 05:22
  • https://stackoverflow.com/questions/1665120/how-can-i-get-all-the-inherited-classes-of-a-base-class and https://stackoverflow.com/questions/5411694/get-all-inherited-classes-of-an-abstract-class/6944605 for code based ways.. I figure you'll only do it once/put the output in a text file.. – Caius Jard Jun 19 '21 at 05:30
  • 1
    No, there is no possibility to restrict intellisense to types allowed for a generic type parameter. The entry will scroll down a list of all types. You can send a [feature suggestion](https://learn.microsoft.com/visualstudio/ide/suggest-a-feature) to the .NET development team to *be able to filter on where clauses*. It's a good, intuitive and logic idea. Otherwise, indeed, you can take a llok on ReSharper or [TypeHierarchyViewer](https://marketplace.visualstudio.com/items?itemName=munyabe.TypeHierarchyViewer). –  Jun 19 '21 at 05:40
  • that would be a nice improvement. Also like just searching for a class implementing an interface that would be great, like `IPoint a = new ` and then there is a proposal of differen IPoint implementations – user287107 Jun 19 '21 at 11:25

0 Answers0