Search Unity

Run bit of code if namespace exists C#

Discussion in 'Scripting' started by elmar1028, Oct 23, 2016.

  1. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,359
    Hello guys,

    I am developing an editor extension that is going to check if another asset exists in the project. So in order to do that I have decided to check if namespace(s) exist before actually running the code, in order to avoid compilation errors.

    Any ideas?

    Thanks!
     
    ROBYER1 likes this.
  2. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    As far as I know there's no way to check assemblies for namespaces, but I think you can do something like this, as long as you don't mind it being a bit slow. Be sure to include the System and System.Reflection namespaces first.
    Code (CSharp):
    1. public bool NamespaceExists(string desiredNamespace)
    2. {
    3.     foreach(Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
    4.     {
    5.         foreach(Type type in assembly.GetTypes())
    6.         {
    7.             if (type.Namespace == desiredNamespace)
    8.                 return true;
    9.         }
    10.     }
    11.     return false;
    12. }
     
  3. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,359
    Will this method work with classes as well?
     
  4. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    The "types" in the second loop are the classes belonging to each assembly, along with interfaces, enums, etc... so you can just check the type name you want against type.Name in that loop- I'd only do it if type.isClass is true though, since as I mentioned it includes other types as well.

    You should go read the chapter in pretty much every C# book dedicated to reflection (C# Essentials, C# in a Nutshell, to name a couple books with good information) as it's a pretty interesting topic. Just be aware that Unity doesn't bother including things in the build that it can't tell are actually used in the program (classes, methods, assets), and reflection bypasses those checks, so if you use it outside of an editor extension you could be inviting problems down the line. It also gets hellishly complicated, the further you delve into it.
     
  5. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,359
    Fortunately, I am writing an editor extension that checks if another asset is inside of the project, so I do not need to check it at runtime. Phew!

    Thanks for the help :)
     
    ROBYER1 likes this.
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    That's reflection in a nut shell.

    It's not too bad once you get used to it. But it requires some weird metal tricks to get used to the idea of treating the code you just wrote a second ago as a variable. And then you realise that your variable is manipulating other variables. And if all code is variables, does that mean that all your variables are code?

    In reality it's all just ones and zeros in memory somewhere. But reflection forces you to stare that fact in the face.
     
  7. amrittb

    amrittb

    Joined:
    Oct 9, 2017
    Posts:
    3
    I modified the given code to check for class names. However, it does not seem to work with basic data types like 'int', 'float' etc.

    Code (CSharp):
    1.     private bool HasType(string typeName) {
    2.         foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies()) {
    3.             foreach (Type type in assembly.GetTypes()) {
    4.                 if (type.Name == typeName)
    5.                     return true;
    6.             }
    7.         }
    8.  
    9.         return false;
    10.     }
    Any other solution?
     
  8. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,528
    'int' and 'float' are syntax shortcuts for the types System.Int32 and System.Single:
    https://msdn.microsoft.com/en-us/library/system.int32(v=vs.110).aspx
    https://msdn.microsoft.com/en-us/library/system.single(v=vs.110).aspx

    This trick of searching types by name searches for their real names, not the syntax sugar versions of their names.

    Also... why would you need to check if 'int' or 'float' exist? These are such fundamental types in C#, so much so that the language has names for them built in, that the idea of them NOT existing is ludicrous.
     
    Ryiah, TeagansDad and Kiwasi like this.
  9. amrittb

    amrittb

    Joined:
    Oct 9, 2017
    Posts:
    3
    I don't want to check if 'int' or 'float' exists. I want to check if a given string such as "int" or "integer" or "Vector3" matches existing types in the assembly.
     
  10. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,528
    Ok captain pedantic... int, integer, and float still aren't types in the assembly. They're called Int32 and Single.
     
    Psyco92 and Ryiah like this.
  11. ROBYER1

    ROBYER1

    Joined:
    Oct 9, 2015
    Posts:
    1,454
    This is just what I needed also, thankyou! Am making an Editor only asset/tool that needs to check if another asset exists in the project files for certain features :)