开发者

How to get a name of the element of the project?

I am interested both in built-in feature or a workaround, it does not matter.

My problem is similar to the question "how to get a name of the field/property of the object" -- so you could not write

  "MyField" 

anymore, but

  NameOf(MyField)

Now, one level up -- and you have not a class, but a project, and not a field, but element of the project -- report, dataset, and so on. I would like to switch from writing:

  LoadReport("MyReport.rdlc");

to

  LoadReport(NameOf(Project.MyReport)+".rdlc");

Of course this is rough idea.

If you wonder why -- because when r开发者_如何学Pythonefactoring, I would like to have all names replaced automatically.

The question is -- is this possible, and if yes -- how?

Edits

  1. Please note refactoring is done in design-time, so getting the actual name can be done in runtime, but binding to it has to be done in design time, not in runtime (so pure reflection is ruled out).


You can check at runtime which assemblies are loaded (since every project is an assembly) with AppDomain.CurrentDomain.GetAssemblies(). You could add an Attribute to all your assemblies and then filter the assembly list to contain only the assemblies with this attribute. That would get you a list of all your currently (at runtime) loaded assemblies.

--- Edit to answer the comment ---

The following code is a quick example of how that could look like:

Attribute declaration (has to be in the 'main' project):

[AttributeUsage(AttributeTargets.Assembly)]
public sealed class ReportPluginAttribute : Attribute { }

Set the attribute on the projects you want to find (Properties/AssemblyInfo.cs would be a good place):

[assembly: ReportPluginAttribute()]

Now you can check for those assemblies:

var reportPlugins = AppDomain.CurrentDomain
    .GetAssemblies()
    .Where(a => a.GetCustomAttributes(typeof(ReportPluginAttribute), false).Any())

foreach (reportPluginAssembly in reportPlugins)
    LoadReport(reportPluginAssembly.GetName().Name+".rdlc");

I'm not quite sure what your LoadReport function is supposed to do. Are all those supposedly Project-specific Reports usable by a single Method? If they have to be loaded by code from inside the Plugin, you could extend the Attribute to carry the information which class to use to load the report.


You can take a look at Get a list of Solution/Project Files for VS Add-in or DXCore Plugin but if you want to use it like Project.MyReport write your own class with mappings.


Think you will need to use reflection and the PropertyInfo class:

http://msdn.microsoft.com/en-us/library/system.reflection.propertyinfo.aspx


I think this might be what you are looking for (reflection).

To access the program itself at runtime:

Assembly assembly = Assembly.GetExecutingAssembly();

Or more specifically, to access the files in you project directory itself:

DirectoryInfo di = new DirectoryInfo(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)); // access the main project folder

if (di != null)
{
    FileInfo[] subFiles = di.GetFiles("*.rdlc"); // get only the files with extention  rdlc, later you can output them using a foreach loop
}

I hope that helps :)


Why not use reflection and get the name of the assembly and/or class and store these in a static cache?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜