问题
I have VS 2008 and csproj project (C# Library).
In properties of project, has an assembly name, and default namespace. Note: each class has a namespace.
Is it possible, in runtime, get the value of default namespace ??
My target is use resources, and I need default namespace's value:
Assembly assembly = Assembly.GetExecutingAssembly();
//foreach (string resourceName in assembly.GetManifestResourceNames()){}
Stream syntaxModeStream = assembly.GetManifestResourceStream(pathToResources
+ ".SyntaxModes.xml");
Update:
Pieter said I can't. Default namespace don't stored in assembly
var resource = assembly.GetManifestResourceNames().Single(p => p.EndsWith(".SyntaxModes.Xml"))
where is the default namespace stored ??
Only can I read using Addins Visual Studio (DTE object) ??
回答1:
You can't.
The default namespace isn't stored anywhere within the assembly. It's just a project setting of Visual Studio.
What I tend to do is use the name of the assembly: Assembly.GetName().Name
. The problem with this is that it only works if the assembly name has not been changed.
For your specific issue, you can use assembly.GetManifestResourceNames()
and do some tests over those names, e.g.:
string name = assembly.GetManifestResourceNames().Single(p => p.EndsWith(".SyntaxModes.Xml"))
回答2:
Not 100% sure it's what you're looking for but you can get the namespace using :
this.GetType().Namespace
回答3:
I wrote an extension method for Assembly that just finds the resource by its filename within the included resources:
public static Stream GetManifestResourceStreamByFileName(this Assembly assembly, string fileName)
{
var targetName = assembly.GetManifestResourceNames()
.FirstOrDefault(x => x.EndsWith("." + fileName));
return targetName == null ? null : assembly.GetManifestResourceStream(targetName);
}
then you just call like so:
var assembly = Assembly.GetExecutingAssembly(); // or Assembly.GetAssembly(this.GetType()) or whatever
var myResource = assembly.GetManifestResourceStreamByFileName("myResource.jpg");
回答4:
I came to this page in search of confirmation that I hadn't overlooked anything in my search for an assembly property that corresponds to the root namespace entered into the main Visual Studio project property page of a Windows application.
Since I had an idea for solving the problem for the use case that brought me to this page, and I anticipate that at least a few others would benefit from my discovery, I kept the page open while I conducted my research.
As I suspected, it is relatively easy to associate a meaningful, not to mention ultra useful, namespace with the entry assembly. The following very long expression returns the namespace of the Program class defined in the Program.cs module of any Windows application.
System.Reflection.Assembly.GetEntryAssembly ( ).EntryPoint.DeclaringType.Namespace )
The following C# statement, executed from anywhere in the application domain, even through a method call that reaches into a DLL, reports the name of the entry point routine and its namespace.
Console.WriteLine (
"Namespace of Entry Point Routine {0} = {1} " ,
System.Reflection.MethodBase.GetCurrentMethod ( ).Name ,
System.Reflection.Assembly.GetEntryAssembly ( ).EntryPoint.DeclaringType.Namespace );
For a console mode program called OperatingParameters_Demo.exe, whose root namespace is OperatingParameters_Demo, the above statement yields the following output.
Namespace of Entry Point Routine Main = OperatingParameters_Demo
The practical use of this expression is that you can use it to construct the absolute (fully qualified) name of the Properties.Settings and Properties.Resources namespaces of the entry assembly.
- Access to the settings from anywhere in the application means that you can search for a setting, the name of which is unknown until run time.
- Access to the resources from anywhere in the application means that string resources that are stored in the entry assembly or its satellite resource assemblies are accessible from throughout the application domain, even if their names are unknown until run time.
I am almost finished with an application that takes advantage of this technique to display parameter names that are stored in resource strings based on resource names that are derived from the parameter's internal name. This lies at the heart of the generic program parameter processing library that the tool demonstrates.
This demonstration project began when I started work on a programmer's tool to import C/C++ header files into a project directory, so that I can deploy a self-contained project into GitHub, even though the master copies of the headers are stored elsewhere because they are shared by dozens of projects. As its parameter parsing engine neared completion, I realized that I was incredibly close to having the generic operating parameter parser and backing store that I have wanted for many years. The C/C++ header importer will eventually be published. However, at the moment, only its parsing engine is finished. Since it is useful on its own, I intend to publish it first. Stay tuned to The Code Project and GitHub.
来源:https://stackoverflow.com/questions/4885888/how-get-the-default-namespace-of-project-csproj-vs-2008