Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve the class name conflict problem in C#

Tags:

namespaces

c#

I use a third party dll which does not use namespace, it contains a enum named Speaker.

// Decompiled with JetBrains decompiler
// Type: Speaker
// Assembly: StreamSDK, Version=1.0.6782.19546, Culture=neutral,PublicKeyToken=null
// MVID: 82353EB3-505A-4A47-8EEB-ED74ED4FC9B9
// Assembly location: /Users/haha/test/Assets/_ThirdParty/SteamSDK/Core/XMLSerializer/StreamSDK.dll

public enum Speaker
{
  remote,
  local,
  none,
}

My local project also has this class name under a specified namespace Photon.Voice.Unity;. After I import the dll, error happens because the compiler treats the local Speaker as the third party's Speaker.

I already use namespace in my local project:

using Photon.Voice.Unity;

Error happens in the following codes :

private void OnSpeakerCreated(Speaker speaker)
{
    speaker.gameObject.transform.SetParent(this.RemoteVoicesPanel, false);
}

The error:

error CS1061: 'Speaker' does not contain a definition for 'gameObject' and no accessible extension method 'gameObject' accepting a first argument of type 'Speaker' could be found (are you missing a using directive or an assembly reference?)

After I add the full namespace, the codes are passed.

private void OnSpeakerCreated(Photon.Voice.Unity.Speaker speaker)
{
    speaker.gameObject.transform.SetParent(this.RemoteVoicesPanel, false);
}

But I don't want to do that I just want to ban the use of the third party Speaker in the specified cs files or any other ways that I don't need to change the current codes.

like image 565
Harlan Chen Avatar asked Nov 18 '25 09:11

Harlan Chen


1 Answers

If you are too lazy to write Photon.Voice.Unity.Speaker every time, you can create an alias for using a using alias directive:

using PhotonSpeaker = Photon.Voice.Unity.Speaker;

Now you can write:

private void OnSpeakerCreated(PhotonSpeaker speaker)
{
    speaker.gameObject.transform.SetParent(this.RemoteVoicesPanel, false);
}
like image 89
Sweeper Avatar answered Nov 19 '25 23:11

Sweeper