0

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 refactoring, 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).
Community
  • 1
  • 1
greenoldman
  • 16,895
  • 26
  • 119
  • 185

5 Answers5

1

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.

Community
  • 1
  • 1
Silx
  • 2,663
  • 20
  • 21
  • Thank you very much, it seems it will be tougher than I thought because the Project is not subtyped for each project, so all info are available at runtime, not design-time. I will dig deeper, anyway thank you! – greenoldman Mar 12 '11 at 12:05
1

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.

xod
  • 589
  • 3
  • 5
  • I am lost -- how you set an attribute to a report? And second how you call NameOf for your report? You have to have a symbolic name of some kind. – greenoldman Mar 12 '11 at 17:13
  • Actually you gave a good answer, there is no answer :-) All you/me can do is iterate over SOME elements, but you cannot refer to a report via symbolic name. One solution would be setting a label for an attribute, and then fetching report (via attributes) which label is A, B or C, and so on. But this would add more problems to keeping all the "references" up to date. Thank you for answering! – greenoldman Mar 13 '11 at 17:50
0

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

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

Burt
  • 7,680
  • 18
  • 71
  • 127
0

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

slang
  • 626
  • 7
  • 26
Daniel
  • 8,133
  • 5
  • 36
  • 51
0

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 :)

Tim Stone
  • 19,119
  • 6
  • 56
  • 66
MimiEAM
  • 2,505
  • 1
  • 26
  • 28
  • Thank you but to list *.rdlc you don't have to use reflection. But it is less important -- assume you already have such list, and then what do you do with it? How do you get this one "MyReport" using symbolic name? – greenoldman Mar 12 '11 at 17:14