Check If a Type Is from a Particular Namespace

Check if an object's type is from a particular namespace

You can inspect the Type.Namespace property.

e.OriginElement.GetType().Namespace

Check if a type is from a particular namespace

There is a (compiler specific) way to test whether a type is in a certain namespace, but I'll let you decide whether it is better than yours or not:

#include <utility>
#include <type_traits>

namespace helper
{
class ctstring
{
public:
constexpr ctstring(const char* string) : _string(string)
{
}

constexpr const char* c_str() const
{
return _string;
}

constexpr bool begins_with(const ctstring other) const
{
return !*other.c_str() ||
(*_string && *_string == *other.c_str() &&
ctstring(_string + 1).begins_with(other.c_str() + 1));
}

private:
const char* _string;
};

template <typename T>
constexpr bool is_type_in_namespace(const ctstring name)
{
#if defined(_MSC_VER)
#define PRETTY_FUNCTION_OFFSET_1 \
(sizeof("void __cdecl helper::is_type_in_namespace<struct ") - 1)
#define PRETTY_FUNCTION_OFFSET_2 \
(sizeof("void __cdecl helper::is_type_in_namespace<class ") - 1)

return ctstring(__FUNCSIG__ + PRETTY_FUNCTION_OFFSET_1).begins_with(name) ||
ctstring(__FUNCSIG__ + PRETTY_FUNCTION_OFFSET_2).begins_with(name);

#undef PRETTY_FUNCTION_OFFSET_1
#undef PRETTY_FUNCTION_OFFSET_2
#elif defined(__clang__)
return ctstring(__PRETTY_FUNCTION__ +
(sizeof("bool helper::is_type_in_namespace(const "
"helper::ctstring) [T = ") -
1))
.begins_with(name);
#elif defined(__GNUC__)
return ctstring(__PRETTY_FUNCTION__ +
(sizeof("constexpr bool "
"helper::is_type_in_namespace(helper::ctstring) "
"[with T = ") -
1))
.begins_with(name);
#else
#error "Your compiler is not supported, yet."
#endif
}
}

// -- Test it

namespace sample
{
struct True_X;

class True_Y;

template <typename>
class True_T;

template <typename A>
using True_U = True_T<A>;
}

struct False_X;

class False_Y;

template <typename>
class False_T;

template <typename A>
using False_U = False_T<A>;

void test1()
{
static_assert(helper::is_type_in_namespace<sample::True_X>("sample::"), "1");
static_assert(helper::is_type_in_namespace<sample::True_Y>("sample::"), "2");
static_assert(helper::is_type_in_namespace<sample::True_T<int>>("sample::"), "3");
static_assert(helper::is_type_in_namespace<sample::True_U<int>>("sample::"), "4");
static_assert(!helper::is_type_in_namespace<False_X>("sample::"), "5");
static_assert(!helper::is_type_in_namespace<False_Y>("sample::"), "6");
static_assert(!helper::is_type_in_namespace<False_T<int>>("sample::"), "7");
static_assert(!helper::is_type_in_namespace<False_U<int>>("sample::"), "8");
}

namespace sample
{
void test2()
{
static_assert(helper::is_type_in_namespace<True_X>("sample::"), "1");
static_assert(helper::is_type_in_namespace<True_Y>("sample::"), "2");
static_assert(helper::is_type_in_namespace<True_T<int>>("sample::"), "3");
static_assert(helper::is_type_in_namespace<True_U<int>>("sample::"), "4");
static_assert(!helper::is_type_in_namespace<::False_X>("sample::"), "5");
static_assert(!helper::is_type_in_namespace<::False_Y>("sample::"), "6");
static_assert(!helper::is_type_in_namespace<::False_T<int>>("sample::"), "7");
static_assert(!helper::is_type_in_namespace<::False_U<int>>("sample::"), "8");
}

namespace inner
{
void test3()
{
static_assert(helper::is_type_in_namespace<::sample::True_X>("sample::"), "1");
static_assert(helper::is_type_in_namespace<::sample::True_Y>("sample::"), "2");
static_assert(helper::is_type_in_namespace<::sample::True_T<int>>("sample::"), "3");
static_assert(helper::is_type_in_namespace<::sample::True_U<int>>("sample::"), "4");
static_assert(!helper::is_type_in_namespace<::False_X>("sample::"), "5");
static_assert(!helper::is_type_in_namespace<::False_Y>("sample::"), "6");
static_assert(!helper::is_type_in_namespace<::False_T<int>>("sample::"), "7");
static_assert(!helper::is_type_in_namespace<::False_U<int>>("sample::"), "8");
}
}
}

void test4()
{
using namespace sample;

static_assert(helper::is_type_in_namespace<True_X>("sample::"), "1");
static_assert(helper::is_type_in_namespace<True_Y>("sample::"), "2");
static_assert(helper::is_type_in_namespace<True_T<int>>("sample::"), "3");
static_assert(helper::is_type_in_namespace<True_U<int>>("sample::"), "4");
}

int main(int argc, char* argv[])
{
test1();
sample::test2();
sample::inner::test3();
test4();
return 0;
}

I tested this for MSVC2015 and some random online Clang compiler and GCC 6.1.0.

Thoughts:

  • The test accepts classes and structs from namespace sample and any sub-namespace.
  • It doesn't suffer the drawbacks of your solution
  • You might want to build in std::decay_t to remove CV qualifiers.
  • Obviously the code requires >=C++14 Edit: Not any more, C++11 is enough
  • Nobody likes macros Edit: Removed most macros
  • The code isn't very portable and most likely needs additional branches for certain compilers and compiler versions. It is up to your requirements if the solution is acceptable

Edit: Refactored code to make it more clear and added GCC support. Also, the namespace to test for can now be passed as a parameter

Check if a type belongs to a namespace without hardcoded strings

You can define this in the namespace where you want to perform the check:

static class Namespace
{
public static bool Contains<T>()
=> typeof (T).Namespace == typeof (Namespace).Namespace;
}

For example:

namespace My.Inner
{
static class Namespace
{
public static bool Contains<T>()
=> typeof (T).Namespace == typeof (Namespace).Namespace;
}
}

Two types as test cases:

namespace My
{
class SomeTypeA { }
}

namespace My.Inner
{
class SomeTypeB { }
}

Here is the usage:

Console.WriteLine(My.Inner.Namespace.Contains<SomeTypeA>()); // False
Console.WriteLine(My.Inner.Namespace.Contains<SomeTypeB>()); // True

Checking if Class type is available in Javascript Namespace

You should be able to do:

if (!!GClientEncoder)

or:

if (typeof(GClientEncoder) !== "undefined")

How to get type from different namespace than System in c#?

If you want to make this work, you'll have to use the fully qualified type name (including the assembly). For System.Drawing.Color that would be (for .Net 4.0):

System.Drawing.Color, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Type t = Type.GetType("System.Drawing.Color, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");

to get the fully qualified name of an already loaded type, use

t.AssemblyQualifiedName

C# - How to check if namespace, class or method exists in C#?

You can use Type.GetType(string) to reflect a type. GetType will return null if the type could not be found. If the type exists, you can then use GetMethod, GetField, GetProperty, etc. from the returned Type to check if the member you're interested in exists.

Update to your example:

string @namespace = "MyNameSpace";
string @class = "MyClass";
string method= "MyMEthod";

var myClassType = Type.GetType(String.format("{0}.{1}", @namespace, @class));
object instance = myClassType == null ? null : Activator.CreateInstance(myClassType); //Check if exists, instantiate if so.
var myMethodExists = myClassType.GetMethod(method) != null;

Console.WriteLine(myClassType); // MyNameSpace.MyClass
Console.WriteLine(myMethodExists); // True

This is the most efficient and preferred method, assuming the type is in the currently executing assembly, in mscorlib (not sure how .NET Core affects this, perhaps System.Runtime instead?), or you have an assembly-qualified name for the type. If the string argument you pass to GetType does not satisfy those three requirements, GetType will return null (assuming there isn't some other type that accidentally does overlap those requirements, oops).


If you don't have the assembly qualified name, you'll either need to fix your approach so you do or perform a search, the latter being potentially much slower.

If we assume you do want to search for the type in all loaded assemblies, you can do something like this (using LINQ):

var type = (from assembly in AppDomain.CurrentDomain.GetAssemblies()
from type in assembly.GetTypes()
where type.Name == className
select type);

Of course, there may be more to it than that, where you'll want to reflect over referenced assemblies that may not be loaded yet, etc.

As for determining the namespaces, reflection doesn't export those distinctly. Instead, you'd have to do something like:

var namespaceFound = (from assembly in AppDomain.CurrentDomain.GetAssemblies()
from type in assembly.GetTypes()
where type.Namespace == namespace
select type).Any()

Putting it all together, you'd have something like:

var type = (from assembly in AppDomain.CurrentDomain.GetAssemblies()
from type in assembly.GetTypes()
where type.Name == className && type.GetMethods().Any(m => m.Name == methodName)
select type).FirstOrDefault();

if (type == null) throw new InvalidOperationException("Valid type not found.");

object instance = Activator.CreateInstance(type);

how to find out if a certain namespace is used, and if so, where?

The problem is in the last method:

public int getFailedFieldsCount() {
return ListOfFailedFieldsInOneRecord.Count();
}

The method Count() is not a member of a List(T). The property Count, however, is. If you replace Count() by Count, this will compile without the need for using System.Linq.

By including System.Linq, you enable the extension method Count(), which, confusingly enough, does exactly the same thing.

See List(T) Members on msdn for a breakdown of what's part of a List(T).

How to check if string is a namespace

Your question "How to check if string is a namespace" is only valid when you consider where you are checking for namespaces.

Namespaces are prefixes to class names, and classes are scoped to an assembly. To check whether a namespace exists, you need to decide which assemblies you are prepared to look through to find the existence of the namespace.

Once you have decided which assemblies you are prepared to look through, you can iterate through them for the existence of a particular namespace like so:

public bool NamespaceExists(IEnumerable<Assembly> assemblies, string ns)
{
foreach(Assembly assembly in assemblies)
{
if(assembly.GetTypes().Any(type => type.Namespace == ns))
return true;
}

return false;
}


Related Topics



Leave a reply



Submit