How do I get the primary namespace in an assembly? [duplicate] - c#

Given an instance of System.Reflection.Assembly.

I have come across this dilemma plenty of times when I want to load a resource from the current assembly by its manifest resource stream.
The fact is that if you embed a file as a resource in your assembly using Visual Studio its manifest resource name will be derived from the default namespace of the assembly as defined in the Visual Studio project.
The best solution I've come up with (to avoid hardcoding the default namespace as a string somewhere) is to simply ensure your resource loading code is ALWAYS happening from inside a class that's also in the default namespace and then the following near-generic approach may be used.
This example is loading an embedded schema.
XmlSchema mySchema;
string resourceName = "MyEmbeddedSchema.xsd";
string resourcesFolderName = "Serialisation";
string manifestResourceName = string.Format("{0}.{1}.{2}",
this.GetType().Namespace, resourcesFolderName, resourceName);
using (Stream schemaStream = currentAssembly.GetManifestResourceStream(manifestResourceName))
mySchema = XmlSchema.Read(schemaStream, errorHandler);
See also: How to get Namespace of an Assembly?
Edit: Also noticed a very detailed answer to the question I'm answering at http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/3a469f5d-8f55-4b25-ac25-4778f260bb7e
Another edit in case people with same question come looking: Excellent idea to solve the resource-loading question here: How get the default namespace of project csproj (VS 2008)

Not possible. Nothing specifies a "Root" namespace. The default namespace in the options is a visual studio thing, not a .net thing

There could be any number of namespaces in a given assembly, and nothing requires them to all start from a common root. The best you could do would be to reflect over all the types in an assembly and build up a list of unique namespaces contained therein.

I just created an empty internal class called Root and put it in the project root (assuming this is your root namespace). Then I use this everywhere I need the root namespace:
typeof(Root).Namespace;
Sure I end up with an unused file, but it's clean.

Assemblies don't necessarily have a root namespace. Namespaces and Assemblies are orthogonal.
What you may be looking for instead, is to find a type within that Assembly, and then find out what its namespace is.
You should be able to accomplish this by using the GetExportedTypes() member and then using the Namespace property from one of the returned Type handles.
Again though, no guarantees all the types are in the same namespace (or even in the same namespace hierarchy).

I use typeof(App).Namespace in my WPF application.
App class is mandatory for any WPF application and it's located in root.

Get Types gives you a list of Type objects defined in the assembly. That object has a namespace property. Remember that an assembly can have multiple namespaces.

GetType(frm).Namespace
frm is the startup Form

Namespaces have nothing to do with assemblies - any mapping between a namespace and the classes in an assembly is purely due to a naming convention (or coincidence).

There actually is an indirect way to get it, by enumerating the names of the assembly's manifest resources. The name you want ends with the part of it that you know.
Rather than repeat the code here, please see get Default namespace name for Assembly.GetManifestResourceStream() method

The question I had that landed me here was, "If I call library code N methods deep and want the namespace of the Project - for example the MVC app that's actually running - how do I get that?"
A little hacky but you can just grab a stacktrace and filter:
public static string GetRootNamespace()
{
StackTrace stackTrace = new StackTrace();
StackFrame[] stackFrames = stackTrace.GetFrames();
string ns = null;
foreach(var frame in stackFrames)
{
string _ns = frame.GetMethod().DeclaringType.Namespace;
int indexPeriod = _ns.IndexOf('.');
string rootNs = _ns;
if (indexPeriod > 0)
rootNs = _ns.Substring(0, indexPeriod);
if (rootNs == "System")
break;
ns = _ns;
}
return ns;
}
All this is doing is getting the stacktrace, running down the methods from most recently called to root, and filtering for System. Once it finds a System call it knows it's gone too far, and returns you the namespace immediately above it. Whether you're running a Unit Test, an MVC App, or a Service, the System container is going to be sitting 1 level deeper than the root namespace of your Project, so voila.
In some scenarios where System code is an intermediary (like System.Task) along the trace this is going to return the wrong answer. My goal was to take for example some startup code and let it easily find a class or Controller or whatever in the root Namespace, even if the code doing the work sits out in a library. This accomplishes that task.
I'm sure that can be improved - I'm sure this hacky way of doing things can be improved in many ways, and improvements are welcome.

Adding to all the other answers here, hopefully without repeating information, here is how I solved this using Linq. My situation is similar to Lisa's answer.
My solution comes with the following caveats:
You're using Visual Studio and have a Root Namespace defined for your project, which I assume is what you're asking for since you use the term "root namespace"
You're not embedding interop types from referenced assemblies
Dim baseNamespace = String.Join("."c,
Me.GetType().Assembly.ManifestModule.GetTypes().
Select(Function(type As Type)
Return type.Namespace.Split("."c)
End Function
).
Aggregate(Function(seed As String(), splitNamespace As String())
Return seed.Intersect(splitNamespace).ToArray()
End Function
)
)

Here as a rather simple way to get the root namespace for a website project.
''' <summary>
''' Returns the namespace of the currently running website
''' </summary>
Public Function GetWebsiteRootNamespace() As String
For Each Asm In AppDomain.CurrentDomain.GetAssemblies()
If Asm Is Nothing OrElse Asm.IsDynamic Then Continue For
For Each Typ In Asm.GetTypes
If Typ Is Nothing OrElse Typ.Name Is Nothing Then Continue For
If Typ.Name = "MyProject" Then Return Typ.Namespace.Split("."c)(0)
Next
Next
Return Nothing
End Function
This simply checks all the loaded assemblies for the "MyProject" type and returns the root namespace for that type. This is useful for logging when you have multiple web projects in a single solution sharing a log system. Hope this helps someone.

This solution works if you are trying to load an embedded resource.
var assembly = System.Reflection.Assembly.GetExecutingAssembly();
string[] resourceNames = assembly.GetManifestResourceNames();
string resourceNameNoNamespace = $"Languages.{languageSupport.IsoCode}.Languages.xml";
var match = resourceNames.SingleOrDefault(rn => rn.EndsWith(resourceNameNoNamespace));

Dim applicationNamespace = TextBeforeFirst(Assembly.GetCallingAssembly().EntryPoint.DeclaringType.Namespace, ".")
Public Function TextBeforeFirst(value As String, expression As String) As String
If String.IsNullOrEmpty(value) Or String.IsNullOrEmpty(expression) Then Return Nothing
Dim index = value.IndexOf(expression)
If index = -1 Then Return Nothing
Dim length = index
Return value.Substring(0, length)
End Function

Related

How can I get the comments for symbols referenced by a CompletionItem?

Coming from this question, I've managed to get all CompletionItem instances available for a specific offset using completionService.GetCompletionsAsync(document, offset);.
So, after querying for completions of "MyString".Len, I get a CompletionItem for the Length method and can then, using the CompletionService, call service.GetDescriptionAsync(document, completionItem) to retrieve "int string.Length { get; }".
But, how can I get the comments for Length, e.g. "Gets the number of characters in the current String object."? And, if easily possible, other information regarding potential overloads?
Assuming that you're adding references to the assemblies using
MetadataReference.CreateFromFile method, you should pass an DocumentationProvider instance as an additional parameter, like this:
MetadataReference.CreateFromFile(path, MetadataReferenceProperties.Assembly, new MyDocumentationProvider(path));
DocumentationProvider is an abstract class, we ended up implementing our own by overriding GetDocumentationForSymbol method and locating appropriate XML node inside XML document.
Looking at Roslyn source code, there is XmlDocumentationProvider class which has an abstract method GetSourceStream (where you're supposed to pass a content of .xml file that stores documentation for .NET assemblies).
Please note that for this feature to work there should be an .xml file with descriptions file next to the assembly (which is normally produced from the source code when you have compile an assembly with Documentation File option set).
For .NET assemblies these files are included as part of SDK, and normally can be found at:
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\vxxx
We use this approach in our Roslyn-based parsers in our AlterNET Studio product. You may find a bit more information about these parsers here.
GetDescriptionAsync can only return a number of overloads, to get list of overloads available at the same position you might need to use Recommender API like this:
var model = document.GetSemanticModelAsync().Result
var symbols = Microsoft.CodeAnalysis.Recommendations.Recommender.GetRecommendedSymbolsAtPositionAsync(model, pos, workspace).Result;
This API will return a separate symbol for every overload.
We asked a while ago whether it's possible to retrieve additional information (such as underlying symbol) from CompletionItem and the short answer is no. You may refer to the discussion here:
https://github.com/dotnet/roslyn/issues/57677

Class library to utilize invoking namespace

I'm trying to understand reflection in more detail. The project I'm working on is intended to be an internal package, consumed by multiple developers. The problem, we parse a lot of data from varying departments. With documents that have varying headers, ordering, and often abbreviations of the name within the header.
Example: (Delimited Example)
Department A :
Date, Volume, Depth, Turbidity
Date and Time, Volume, Turbidity, Depth
Date, Vol., Turb., Dep.
Date, NTU, Vol, Dep ft
Department B:
Date/Time, Flow, Level, Velocity
Timestamp, Lvl, Flow, Vel.
So in the library I wanted to include a mapping file, with a method that in essence would be called GetHeaderConfigurations. Whomever references this library would be able to call the library with a user friendly name, but would pass an object with certain information.
The important piece would be their object and a namespace. Which I would recurse the namespace for classes, ones that are details about the header within the file.
The problem:
public static IEnumerable<Type> GetHeaderConfiguration(FileConfiguration configuration)
{
var assembly = Assembly.Load(configuration.HeadersNamespace);
// Some more code
}
When I call that from another application that references the library, I don't receive all of the classes. The application builds the object, then stores the namespace in the following manner "Sample.Headers.LabConfiguration". I execute the following:
var headers = Assembly.GetExecutingAssembly()
.GetTypes()
.Where(t => t.IsClass && t.Namespace == "Sample.Headers");
The above does return all the classes within the directory in the solution, but for some odd reason when I attempt to load the assembly the code fails.
I believe the issue is when I call Assembly.Load I'm passing a namespace, not an assembly. Which leads me to the root of the question I'm hoping to understand. How can I successfully use that namespace? How can I load the internal properties?
Assembly.Load() does just that, loads a new assembly (roughly speaking, a DLL or EXE). If you just want to look for classes within the current assembly, use Assembly.GetExecutingAssembly() to get the currently executing Assembly object, and then call GetTypes() on that.
var assembly = Assembly.GetCurrentlyExecutingAssembly();
var headers = assembly.GetTypes().Where(...);
MSDN has the following note regarding performance:
For performance reasons, you should call this method only when you do not know at design time what assembly is currently executing. The recommended way to retrieve an Assembly object that represents the current assembly is to use the Type.Assembly property of a type found in the assembly, as the following example illustrates.
Unless you're running this code many times in a tight loop, there's no reason to be concerned with performance in my opinion. But if you want to follow MSDN's advice, you can replace Assembly.GetExecutingAssembly() with typeof(ThisClass).Assembly where ThisClass is the name of the class containing the code. But Assembly.GetExecutingAssembly() still works (and doesn't require coupling with the class name).

Get Type of Object from String Value

I am writing a customer class which will read CS files and spit out information based on method names and their various parameters.
This essentially reads each line looking for keys (public, class, etc) and then sees what its all about. Anyway this bit works fine, what I'm having issues with is dealing with various different Types.
So what I need to do is work out whether the type is one found natively in .Net, or something I've created, I'm really not bothered which way round just as long as I have some way of telling.
I've tried Type t = Type.GetType("My.Namespace.Classname"); but this just returns null even with the full namespace and name of my custom class object. However if I was to do the same code but with System.String it works perfectly fine, but I can't really account for each possible namespace in the entire framework. This will mean I need a way to get the type without the full namespace, or know how to check my own custom objects using GetType.
Can anybody provide any suggestions on how to go about this? Even if it was creating a new instance of the objects that would be enough, but again I don't have the full namespace for .Net objects.
Edit: Bit of a background
What I'm doing is reading classes that I've created in a StreamReader, reason being that I'm creating lots of them and need to do making between objects that one system will be able to understand, and another, so this code would read everything and just create the mapping for me. And in most cases this is perfectly fine, it is only when I have custom types, so I want to identify these are mark them.
I've tried Type t = Type.GetType("My.Namespace.Classname"); but this
just returns null
You need to provide the full assembly-qualified name:
Type t = Type.GetType("My.Namespace.Classname, MyAssembly");
From MSDN:
Parameters
typeName
Type: System.String
The assembly-qualified name of the type to get. See AssemblyQualifiedName. If the type is in the currently executing
assembly or in Mscorlib.dll, it is sufficient to supply the type name
qualified by its namespace.
Anyway, if you're looking to parse C# code an analyze it, I would take a look at NRefactory - an open source C# parser -.
Here's an introduction in CodeProject to NRefactory.
I've tried Type t = Type.GetType("My.Namespace.Classname"); but this just returns null even with the full namespace and name of my custom class object.
I suspect that's because it's not in the calling assembly or mscorlib, which are the only two assemblies checked by Type.GetType for names which aren't assembly-qualified.
If you know all the assemblies involved, you could run through each of them calling Assembly.GetType(namespaceQualifiedName) on each of them.
However, if you don't even have the namespace-qualified name, you should possibly create a lookup of all types in all the relevant assemblies, based on their names. For example:
var lookup = assemblies.SelectMany(a => a.GetTypes())
.ToLookup(t => t.Name);
At that point, for each name you have(e.g. Classname in your example) you can find all the types with that name:
foreach (var type in lookup[name])
{
// Do something with type
}
Type.GetType(some_type_name) will return type object if some_type_name is name of type declared any assemblies loaded at the moment, or in Mscorlib.dll
So if your are parsing your types from .cs files and not loading assebly - it will always be null with types names from your source file

How to get a Type from an object in another DLL?

I've got an object called "Communication" that has a method to "CreatePdfFromTemplate". This method is going to be called from a Windows Service that has a SqlDependancy on a table that will notify when a new row is added by a method on a website.
Into my method, I pass a list of custom objects that have an "Id" and a "Name". The name, is the name of the object I need to load using reflection. For example, "Instruction". The "Id" is the Id of the object referred to in "Name" that needs to be loaded from the database. This object is not referenced or available in the runtime of my "Communication" DLL.
I'm currently falling at the first hurdle. I am trying to do the following as a first step:
// Load object information using Reflection
Type objectType = Assembly.GetExecutingAssembly().GetType(queueObject.Name);
int objectId = queueObject.Id;
I have found some info from my searches for answers that say there is a way to load a DLL by making it available in the application cache or the GAC, but I wasn't sure if this was the best way to go.
I've never used Reflection before so if you have any advice on it, or any advice on the way I have chosen to structure this in general (i.e. website adds row to DB table, SqlDependancy in Windows Service fires, calls to Communication service DLL to create PDF).
Just to give you some more information, the reason I have chosen to do it like this, is because my templates contain tags such as {Instruction.CreatedDate} where "Instruction" is the name of the object and "CreatedDate" is the name of a property, the value of which will replace the tag.
Any help on how to load this "Instruction" object in my Reflection or just on my structure in general is much appreciated. Let me know if I haven't given enough info or if what I've said isn't clear enough (this is my first StackOverflow question, although I am a long time lurker).
Thanks.
--UPDATE--
Ok, using the idea put forward from Maarten, I have managed to load my assembly and get a type from it, but I've done it slightly differently. I wasn't able to put in a specific path using the Assembly.LoadFile method, so I've done it like this:
Assembly executingAssembly = Assembly.GetExecutingAssembly();
Assembly objectAssembly = Assembly.Load(executingAssembly
.GetReferencedAssemblies()
.Where(a => a.Name == "Common")
.FirstOrDefault());
This works because the Type I am trying to get, is part of a referenced assembly in my Communication service called "Common" (which is an installed package using nuget to help keep it up to date, as it changes quite often).
Any further posts on how I'm doing this and if it's the right or wrong way would be appreciated though!
Load the assembly using Assembly.LoadFile or another overload.
Get the type using Assembly.GetType.
Use the Activator.CreateInstance once you have the type.
Cast it to dynamic, and call your method, or set your property. I'm assuming you are using .net 4.0.
var myAssembly = Assembly.LoadFile(...);
var myType = myAssembly.GetType(...);
dynamic myObject = Activator.CreateInstance(myType);
if (myObject != null) {
var createdDate = myObject.CreatedDate;
}

Alias and namespace conflict in Visual Studio Designer

I have a namespace conflict between two referenced assemblies:
i.e., I'm referencing Foo.A.Foo and Foo.Bar, so when I say I want Foo.Bar.Control, VS is trying to find Foo.A.Foo.Bar.Control
I can twiddle the Designer.cs code by adding new global:Foo.Bar.Control(), but as soon as I change anything, VS switches back.
I know there's something about adding aliases directly to the reference, I've tried but haven't managed to find the right combination (inline alias, using alias, reference alias).
Help?
"extern alias" may be what you mean, but I'm not sure what the designer will do with it, unfortunately...
I'm not even sure that's what you're after though - that's normally for two types from different assemblies with the same name.
You can write namespace aliases with a using directive, e.g.
using FooControl = Foo.Bar.Control;
but again, the designer is going to rewrite your code...
OK, this isn't the answer, but it's what I found for a workaround:
namespace FooBar
{
class FooBarControlHack : Foo.Bar.Control { }
}
So I can do the following in the Designer.cs :
this.fooBarControl = new FooBar.FoorBarControlHack();

Categories