how to get the assembly in the parent class of an ancestor

D

Dan Holmes

If i have Class2 that is inherited by Class1, how can Class1's methods
operate on Class2's assembly information?

For instance:
public class Component
{
public virtual List<ComponentInfo> GetAssemblyInfo()
{
try
{
List<ComponentInfo> ci = new List<ComponentInfo>();
// how can Component get the assembly of its immediate descendant?
ci.Add(fillComponentInfo(System.Reflection.Assembly.);
return ci;
}
catch (Exception ex)
{
throw;
}
}
public class ThisComponent : Component
{
}

GetCallingAssembly doesn't return the Assembly that ThisComponent lives
in. It returns the assembly that the code that invoked the
GetAssemblyInfo. Also GetExecutingAssembly returns the assembly that
Component lives in. Check out the line above that is
fillComponentInfo(...).

The only thing i can think of now is a protected property that the
descendant sets in the constructor. I really don't want to have to add
that kind of thing to every Class that inherits from Component.

dan
 
G

Greg Young

You could get your current type .. this.GetType() .. you can then use
Type.Assembly to get the assembly the type resides in.

In general I think this should be bringing up red flags from a design
perspective .. any time a parent is worried about its dependants there
should be cause for alarm.

Cheers,

Greg Young
MVP - C#
 
D

Dan Holmes

I added a protected field to the Component class

protected Type descendant

the constructor does as you suggest.

From a design POV, the goal is to get the assembly information and keep
that process in a central place. I was hoping that by inheriting the
Component class there wouldn't be any knowledge of the Component class
nor any code with it by its descendants. I don't think this is
intrusive since it only uses the Type information to get an assembly
that is already in memory and retrieve attributes. I am open to other
ways and designs though.

This is the information that the Component gets and returns. I didn't
want to require an explicit call to a method and was hoping that
inheriting would do that.

AssemblyInfo ci = new AssemblyInfo();
ci.Path = System.IO.Path.GetFullPath(a.Location);
ci.AssemblyFileName = System.IO.Path.GetFileName(a.Location);
ci.Name = a.FullName;
ci.Version =
((AssemblyFileVersionAttribute)Attribute.GetCustomAttribute(a,
typeof(AssemblyFileVersionAttribute))).Version;
ci.Title =
((AssemblyTitleAttribute)Attribute.GetCustomAttribute(a,
typeof(AssemblyTitleAttribute))).Title;
ci.Product =
((AssemblyProductAttribute)Attribute.GetCustomAttribute(a,
typeof(AssemblyProductAttribute))).Product;
ci.Copyright =
((AssemblyCopyrightAttribute)Attribute.GetCustomAttribute(a,
typeof(AssemblyCopyrightAttribute))).Copyright;
ci.CompanyName =
((AssemblyCompanyAttribute)Attribute.GetCustomAttribute(a,
typeof(AssemblyCompanyAttribute))).Company;
ci.Description =
((AssemblyDescriptionAttribute)Attribute.GetCustomAttribute(a,
typeof(AssemblyDescriptionAttribute))).Description;
ci.FileDateTime = System.IO.File.GetLastWriteTime(a.Location);
return ci;
 
G

Greg Young

It should be able to use this.GetType() instead of requiring the descendent
to explicitly state its type.

Type t = this.GetType();
Assembly DescendentAssembly = t.Assembly;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top