Home > Net >  Is it possible to export classes under a new namespace in C#?
Is it possible to export classes under a new namespace in C#?

Time:07-14

I'm pretty new to C#, so I may just not know the terms to search for.

I'm trying to export a subset of classes available in a namespace to manage visibility.

High level - there's an assembly which provides the UnityEngine namespace. UnityEngine is huge. I'm writing various utility libraries which utilize a very small subset of the UnityEngine. A few points:

  1. Including the "wrong" classes in the libraries results in code being untestable (and thus does not get tested).
  2. The main purpose is it's clear to other developers when boundaries are being crossed.
  3. I desire to use the UnityEngine types as opposed to my own so there's not a bunch of conversion. For example Vector2 is a type that should be used everywhere.

So given that, I'm trying to get something like the following to work, though other approaches are welcome too:

... Assembly UnityTypes.asmdef (my code)
... Depends on UnityEngine
namespace UnityTypes {
  using Vector2 = UnityEngine.Vector2;
}

... Assembly PathFinding.asmdef (my code)
... Depends On UnityTypes (my code), NOT UnityEngine
namespace PathFinding {
    using UnityTypes;

    public class GraphBuilder {
        public INode AddNode(Vector2 position, List<INode> edges);
    }
}

... Assembly TheGame.asmdef (my code)
... Depends on Pathfinding and UnityEngine
namespace TheGame {
   using UnityEngine;

   public class PathFinder : MonoBehavior {
       public void Awake() {
          for (var waypoint : GetComponentsInChildren<IWaypoint>) {
            _graph.AddChild(/* Vector2 */ waypoint.position, waypoint.destinations);
          }
       }
   }
}


CodePudding user response:

Are you trying to "change" the classes of an existing library? I do not think that is possible or desirable.

You can inherit from those classes (given they are not marked as sealed) to implement your own behavior.

You can, however, not change existing classes.

CodePudding user response:

It is a bit unclear to me what exactly you are trying to do here.

It sounds a bit like in your first script you want to do

namespace UnityTypes 
{
  using Vector2 = UnityEngine.Vector2;
}

and now you expect that wherever you do

using UnityTypes;

or even wrapping your new code in

namespace UnityTypes
{

}

the other static using is automatically applied.

This is not the case!

The usings in c# a per script.


An alternative sometimes might be using inheritance like e.g.

namespace UnityTypes 
{
    public struct GameObject : UnityEngine.GameObject { }
}

however, afaik most of the types you would be interested in will most probably be sealed or structs (like the Vector2).


=> Your users will simply have to use the correct usings and explicit types where necessary.


Now regarding the using: You only need it in those scripts where you explicitly use the type name.

So in your case GraphBuilder explicitly has a signature with Vector2 so here you either need the

using UnityEngine;

or have to explicitly write

UnityEngine.Vector2

and also need according assembly reference.

Then in your PathFinder you wouldn't need the

using UnityEngine;

when only accessing and passing the waypoint.position, but in your case you need the namespace anyway since MonoBehaviour etc are also part of it ;)

You still will need the assembly reference as soon as you access any member of a type that is defined in it, even if you only pass it on to somewhere else, you don't need to do that in case you only pass on the type which contain fields and properties etc where the type is used.

  • Related