By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,980 Members | 1,302 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,980 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.Assembl y.);
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
May 12 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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#
"Dan Holmes" <da*******@bigfoot.com> wrote in message
news:Od**************@TK2MSFTNGP04.phx.gbl...
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.Assembl y.);
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

May 12 '06 #2

P: n/a
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.GetCustom Attribute(a,
typeof(AssemblyFileVersionAttribute))).Version;
ci.Title =
((AssemblyTitleAttribute)Attribute.GetCustomAttrib ute(a,
typeof(AssemblyTitleAttribute))).Title;
ci.Product =
((AssemblyProductAttribute)Attribute.GetCustomAttr ibute(a,
typeof(AssemblyProductAttribute))).Product;
ci.Copyright =
((AssemblyCopyrightAttribute)Attribute.GetCustomAt tribute(a,
typeof(AssemblyCopyrightAttribute))).Copyright;
ci.CompanyName =
((AssemblyCompanyAttribute)Attribute.GetCustomAttr ibute(a,
typeof(AssemblyCompanyAttribute))).Company;
ci.Description =
((AssemblyDescriptionAttribute)Attribute.GetCustom Attribute(a,
typeof(AssemblyDescriptionAttribute))).Description ;
ci.FileDateTime = System.IO.File.GetLastWriteTime(a.Location);
return ci;
--
dan

Greg Young wrote:
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#
"Dan Holmes" <da*******@bigfoot.com> wrote in message
news:Od**************@TK2MSFTNGP04.phx.gbl...
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.Assembl y.);
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


May 12 '06 #3

P: n/a
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;

"Dan Holmes" <da*******@bigfoot.com> wrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
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.GetCustom Attribute(a,
typeof(AssemblyFileVersionAttribute))).Version;
ci.Title =
((AssemblyTitleAttribute)Attribute.GetCustomAttrib ute(a,
typeof(AssemblyTitleAttribute))).Title;
ci.Product =
((AssemblyProductAttribute)Attribute.GetCustomAttr ibute(a,
typeof(AssemblyProductAttribute))).Product;
ci.Copyright =
((AssemblyCopyrightAttribute)Attribute.GetCustomAt tribute(a,
typeof(AssemblyCopyrightAttribute))).Copyright;
ci.CompanyName =
((AssemblyCompanyAttribute)Attribute.GetCustomAttr ibute(a,
typeof(AssemblyCompanyAttribute))).Company;
ci.Description =
((AssemblyDescriptionAttribute)Attribute.GetCustom Attribute(a,
typeof(AssemblyDescriptionAttribute))).Description ;
ci.FileDateTime = System.IO.File.GetLastWriteTime(a.Location);
return ci;
--
dan

Greg Young wrote:
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#
"Dan Holmes" <da*******@bigfoot.com> wrote in message
news:Od**************@TK2MSFTNGP04.phx.gbl...
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.Assembl y.);
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


May 12 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.