3

I have class RenderManager which has public static member TheRenderer. Instead of RenderManager.TheRenderer can I create alias for that like MyRenderer or whatever?

Thank you

pokoko222
  • 57
  • 1
  • 3

4 Answers4

2

You can create a class or method-level symbol which points to that object, but you can't create a truly global alias which points to that object, no. It would still have to be namespaced in some other object.

Locally, though (inside a function or class) you could do something like var renderer = RenderManager.TheRenderer, but you would have to do that everywhere you want to use that alias.

Steve Hobbs
  • 3,296
  • 1
  • 22
  • 21
1

I don't think you can do this. The only possibility is to make an alias for Type and not for its members, something like this:

using rnd = RendererManager; 

//and in code somewhere use 

rnd.TheRenderer

Hope I right understood what you mean.

Tigran
  • 61,654
  • 8
  • 86
  • 123
  • 1
    What I came to this question looking to ask was whether it would be possible to do something like `using r = RendererManager.TheRenderer;`. Is it possible? The compiler doesn’t seem to let me. – binki Apr 28 '20 at 14:41
0

Do you just mean var MyRenderer = RenderManager.TheRenderer;? i.e. assigning the value of the static member to a variable called MyRenderer?


Not that unlike valipour's solution, if the RenderManager.TheRenderer value changes after you assign it, MyRenderer will have the old value.

George Duckett
  • 31,770
  • 9
  • 95
  • 162
0

You can declare a variable of type Func or Action.

If TheRenderer has return value:

static Func<bool> MyRenderer = () => RenderManager.TheRenderer();

If not:

static Action MyRenderer = () => RenderManager.TheRenderer();

if TheRenderer accepts some input parameters then you should use other forms of Func<> and Action<>.

Func<OutT, In1T, In2T, ...>
Action<In1T, In2T, ...>
Mo Valipour
  • 13,286
  • 12
  • 61
  • 87
  • This works, except that you need to call the `Invoke()` method, i.e. `MyRenderer.Invoke()`, before you can access `RenderManager.TheRenderer()`'s returned value. – Niel de Wet Feb 13 '13 at 14:47