alansiqueira27
alansiqueira27

Reputation: 8506

How do I get Assembly name from full assembly string?

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?

Upvotes: 1

Views: 964

Answers (2)

reptile
reptile

Reputation: 185

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.

Upvotes: 1

Glen Thomas
Glen Thomas

Reputation: 10744

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

Upvotes: 0

Related Questions