How to get hte Assembly when...

  • Thread starter Thread starter Lucas Sain
  • Start date Start date
L

Lucas Sain

Hi,

I have a form (A) which has a collection as a property. All my forms
inhert from this base form.

I'm tring to put some code in form A where I can get the fieldInfo from
the form that imnherits from A. This is the code:

Type t = this.GetType();
FieldInfo parameterInfo = t.GetField(field,BindingFlags.Instance |
BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static |
BindingFlags.FlattenHierarchy);
if(parameterInfo == null)
{
if(Assembly.GetEntryAssembly() != null )
{
MessageBox.Show("Field nod found");
fieldFound = false;
}
}

The problem with this is that it gets the Assembly from Form A (base form)
because its where the collection was called so then the field is never
found. Form A (bas form) is in another assembly/solution. All this has to be
done is design time and not runtime. How can I get the fileds or assembly of
the current form and not the inhertided form.

I hope I made my self clear..

Regards
Lucas
 
I've recreated the situation you've described and haven't
encountered any errors. Perhaps I misunderstood your
problem, so let me explain what I did:

Assembly One
Defines OriginClass with one method, "public string Test
()". "Test" simply uses the code from your example and
returns either the parameterInfo.Name or "Field Not Found"

Assembly Two
Defines DerivedClass which inherits from OriginClass

Assembly Three
Defines a third assembly that defines a DerivedClass
object and calls its "Test" method (which is actually the
method from OriginClass).

Does this accurately model your situation?

Jerry Negrelli
Senior Software Engineer
Data Scientific Corporation
-----Original Message-----
Hi,

I have a form (A) which has a collection as a property. All my forms
inhert from this base form.

I'm tring to put some code in form A where I can get the fieldInfo from
the form that imnherits from A. This is the code:

Type t = this.GetType();
FieldInfo parameterInfo = t.GetField (field,BindingFlags.Instance |
BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static |
BindingFlags.FlattenHierarchy);
if(parameterInfo == null)
{
if(Assembly.GetEntryAssembly() != null )
{
MessageBox.Show("Field nod found");
fieldFound = false;
}
}

The problem with this is that it gets the Assembly from Form A (base form)
because its where the collection was called so then the field is never
found. Form A (bas form) is in another
assembly/solution. All this has to be
 
Jerry,

Yes it does but I'm calling a collection in design time this is where
the problem occurs. It is at design time I want to get the field info from
the derived class. When I execute the code from the parameterInfo.... that
is in the OriginClass... it is looking for the fields in Assembly One and
not Assembly Three... al this at design time.

Regards
Lucas
 
Maybe I'm not uderstanding you correctly, but I think you're looking in the
wrong place for the property. Does this code work for you:

// Assembly1.cs
public class BaseClass
{
int SomeField;
void Test() {
Type t = this.GetType(); // I think this is wrong
t = typeof(BaseClass); // Try this instead
FieldInfo parameterInfo = t.GetField(field,BindingFlags.Instance |
BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static |
BindingFlags.FlattenHierarchy);
if(parameterInfo == null)
{
if(Assembly.GetEntryAssembly() != null )
{
MessageBox.Show("Field not found");
fieldFound = false;
}
}
}
}

// Assembly2.cs
public class DerivedClass : BaseClass
{
}
 
Grant,
Thanks for your Reply.
BaseClass has a method (BaseMehtod) that searches X control on a form
(The code I wrote before).
The problem is that in the DerivedClass I have added some Controls which
I want the BaseMethod to be able to reflect at design time because all this
validation should occure at design time, so if I use your sugestion I only
get the type of the BaseClass so no fields are found. All the fields are in
the DerivedClass.

Am I doins something wrong or am I missing something....

Regards
Lucas Sain
 
Ahhhh! Well, design time is an entirely different beast and well out of my
league.

So you have something like this:
//assembly 1
class BaseClass {
void EnumDerivedFields() {
// code here to find "SomeField"?
}
}

//assembly 2
class DerviedClass : BaseClass {
int SomeField;
}


So basically you have a base class that somehow needs to change according to
which specific derived class instance it is? It sounds like what you should
really be doing is having more virtual methods or properties in the derived
classes...
 
Back
Top