简体   繁体   中英

Using namespace as type error

I am currently translating my java game engine into c#. Every thing is fine until I started writing the package com.gej.graphics . In c#, I wrote it like this.

namespace GECS.Graphics {}

But compiler says this error.

'GECS.Graphics' is a 'namespace' but is used like a 'type' (CS0118)
   - C:\Users\sr....ECS\Core\Game.cs:33,36

This is the text containing at the line

public virtual void Render(Graphics g) {}

The actual Graphics class is from System.Drawing .

My requirement is that this class is to be extended by users and hence they should have no conflicts with it.

Thanks

You can access your class with the Namespace as:

public virtual void Render(System.Drawing.Graphics g) { }

Since there is a conflict in names of your namespace and Graphics class, you may consider naming your namespace different, or you can always use the fully quilified name.

public virtual void Render(System.Drawing.Graphics g) {}

您还需要指定名称空间,因为还要创建一个名称为Graphics的自定义名称空间。

Avoid to create namespace which can conflict with standart namespaces. I your case you should use System.Drawing.Graphics name entirely, to avoid conflicts with GECS.Graphics .

Another solution could be using directive to create an alias for a namespace or a type.

For example:

using DrawingGraphics = System.Drawing.Graphics;

after:

public virtual void Render(DrawingGraphics  g) 
{
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM