1

I have a string like this:

"MyProduct.Framework.Business.ShopBusiness"

But the assembly name it self is only:

"MyProduct.Framework.Business"

I need to create a string like this:

"MyProduct.Framework.Business.ShopBusiness, MyProduct.Framework.Business"

Because I'm trying to solve this problem: https://stackoverflow.com/a/3512351/375422

So basically I have this:

Type executingClass = Type.GetType(TestContext.FullyQualifiedTestClassName);

And I need something like this:

Type executingClass = Type.GetType(TestContext.FullyQualifiedTestClassName + ", " + Assembly.FromString(TestContext.FullyQualifiedTestClassName));

Is there any built-in method as I invented "Assembly.FromString()" above?

In other words: How can I get Assembly Name of the executing test from TestContext?

Community
  • 1
  • 1
alansiqueira27
  • 8,129
  • 15
  • 67
  • 111
  • I only have this: TestContext.FullyQualifiedTestClassName – alansiqueira27 Mar 30 '16 at 22:03
  • 4
    This sounds like an [XY problem](http://xyproblem.info/). What is your actual goal? – Jason Watkins Mar 30 '16 at 22:04
  • my goal is to extract the assembly name without the class. So from this string: "MyProduct.Framework.Business.ShopBusiness", I want: "MyProduct.Framework.Business". Is there any built-in method? – alansiqueira27 Mar 30 '16 at 22:10
  • If that's your ultimate goal, why don't you just take the substring from the beginning of the string up to the last period? – Jason Watkins Mar 30 '16 at 22:11
  • Well, if there isn't any built-in method, that's what I'm going to do. – alansiqueira27 Mar 30 '16 at 22:13
  • Even if there was a way to do this involving the type system, it would be horribly inefficient compared to a simple SubString – Jason Watkins Mar 30 '16 at 22:14
  • 1
    What happens if the namespace of the type is not the same as the assembly name?? Its not gonna work! – Glen Thomas Mar 30 '16 at 22:20
  • @GlenThomas, yeah I was worried about something like this. That's why I'm looking for a built-in method. Thanks for showing this problem. – alansiqueira27 Mar 30 '16 at 22:29
  • http://stackoverflow.com/questions/36320510/how-do-i-get-assembly-name-from-full-assembly-string#comment60264887_36320759 – alansiqueira27 Mar 30 '16 at 22:31
  • 1
    You can't get that in a straightforward way. A type's full name is not enough information to figure out what assembly it is in because namespaces and assemblies are orthogonal. The TestContext class does not contain the necessary information about the running test assembly either. One approach would be to scan a list of assemblies for a type with the same full name. You could get the list of loaded assemblies from the current appdomain or you could register each test assembly in an assembly initialize method and search them. – Mike Zboray Mar 30 '16 at 22:49

2 Answers2

1

There is no direct link between class namespace and assembly name. So, generally, it's not possible to get fully qualified name from class name only.

reptile
  • 175
  • 1
  • 9
0

Find position of last "." and substring to that position.

var fullTypeName = "MyProduct.Framework.Business.ShopBusiness";
var typeIndex = assemblyTypeName.LastIndexOf(".");
var namespace = assemblyTypeName.Substring(0, assemblyIndex);

Then add your namespace:

var assemblyQualifiedTypeName = fullTypeName + ", " + namespace
Glen Thomas
  • 10,190
  • 5
  • 33
  • 65
  • Thanks for helping, but the problem with that is that if "Business" is just a folder, then I can't have it. I can't just assume that only the last part of the string is the assembly name. – alansiqueira27 Mar 30 '16 at 22:23
  • 1
    There is no way to reliably guess the assembly name. You could try using Assembly.GetReferencedAssemblies and then use trial and error to find the right one, but that seems a bit crazy.. and there could be multiple identically named types – Glen Thomas Mar 30 '16 at 22:34