Thursday 15 January 2015

c# - Cannot get types by custom attributes across assemblies -


I am trying to create a basic widget plugin system inside my app and I'm currently on a sticking point where Built in the widget, my use WidgetAttribute attribute class (compiled in exe) work fine but I can not find test plugins in any of the widgets I've written in any of the widgets.

[string] m_filenames = Directory.GetFiles ("plug-in" , "* .dll", SearchOption.TopDirectoryOnly); Foreach (string FN in m_filenames) {Load (Directory.GetCurrentDirectory (+ + "\\" + fn); }} Public Static Zero Load (String Filename) {Assembly m_asm = assembly.Load (File. ReadBelts (filename)); PluginAssemblies.Add (m_asm); }

And then the following code is all my attribute that shows a widget and its meta data to get:

  public static void LoadAllWidgets () {External (AppDomain.CurrentDomain.GetAssemblies (assembly) assembly) {LoadAllWidgets (assembly); } Foreach (PluginLoader.Plugin Assembly Assembly in Assemblies) {LoadAllWidgets (Assembly); }} Public static void LoadAllWidgets {foreach {WidgetAttribute [] m_attribs = (WidgetAttribute []) type.GetCustomAttributes (Incorrect). Type in the type & type (asm.GetTypes () type in lt;); WidgetAttribute & gt; () ToArray (); If (m_attribs.Length & gt; 0) {WidgetsByGuid.Add (m_attribs [0] .iid, type); Widgetnamebigyguide.address (m_tribbiz [0] .iid, m_atrbs [0] .name); WidgetSwineMenuGoogle.Aud (M_Trebij [0] .IID, M_TRBR [0] .ShowInMenu); }}}   

When I call LoadAllWidgets for the second time (ie. Widget that comes from other assemblies), I get type within type array, but Then GetCustomAttributes returns an empty array. The

attribute is defined as, and the same source file is used in both projects:

  using the system; Using the system. Namespace WSW.Classes {[AttributeUsage (AttributeTargets.Class)) Public Category Widgets Attribute: System Attribution {public string name {get} Set; } Get Public Guide ID { Set; } Receive Public Boolean Show Inmenu { Set; } Public WidgetAttibat (String Guide, String Name, Boolean Showmenu) {name = name; ID = Gide. Purse (guide); ShowInMenu = showmenu; } Public Widget Attribute (string guide, string name) {name = name; ID = Gide. Purse (guide); ShowInMenu = true; Can anyone tell me why I can not get custom attributes in a way outside my assembly, even though I have loaded it and can see the type?  

> Edit In another assembly, a plugin class is defined as:

  [WSW.Classes.WidgetAttribute ("{42544B13 -7353-40FD-B1D8-DA46A247110A} "," Testimonial ")] Public Partial Class Testimonials: User Controls (Public Testimonials) () {InitializeComponent (); }} Type of identification {name of the assembly name} type  name of the name}}    

So when you are using "using the same source file in both projects" , it means 2 different assemblies in 2 different assemblies Attributes (which are the same names) Hence the ether assembly will not be able to easily see the features from the second assembly (will see both features, just will not work as a way of comparison).

  • Ensure that each shared feature is compiled in only one assembly and that assembly is used by all consumers of the specialty.
  • Compare the names of the attributes, not the type.

    The same idea applies to other cases when type identification is used ie. If the interface is shared as a source source of 2 assembly compared to these interfaces and the class implementing them will not be considered compatible.

    So often the common types of sharing / interfaces / programs for the calculation and related assemblies are simultaneously shared in the assembly. Such an assembly can also be considered as a public API and can be shared with external programs which are designed to integrate with your program.

    Note that as soon as you share the assembly with someone else, it means that you need a version of the story i.e. changing the name of a type / method) load dependent assemblies for the shared assembly Or may fail to get spoiled.

No comments:

Post a Comment