Pointers, Classes, Objects ...

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

How can I get an object address as IntPtr ?
Somehow even in unsafe it's not working with objetcs...

I'am trying to compare two objects that are not the same but could be from
some point.

Example :

Let say we have two classes one derived from another

public class A
{
protected h=0;

public virtual func()
{
h=0;
}
}

public class B:A
{
public override func()
{
h=1;
}
}


I created two instance of these classes obj1 and obj2,

obj1=Activator.CreateInstance(t1); //Type t1; class A
obj2=Activator.CreateInstance(t2); //Type t2; class B

After invoking we can see in the debugger somthing like this...

obj1.A.h=0
obj2.B.A.h=1;

I dont whant to compare fieald by GetFieald() and comparing them one by one,
My idea was to try the Marshal.ReadIntPtr(IntPtr,offset) so I can read obj2
after some offset, So I needed my variable Address. (somehow even in unsafe
we cant do object *p=obj1; on objetcs...)

I tried Marshel.ReadByte and it's not working cause my object type could be
anything...


Anyone have some idea ???

Thanks
 
I'am trying to compare two objects that are not the same but could be from
some point.

You can see if two variables refer to the same object with

if ( (object)obj1 == (object)obj2 )

or

if ( Object.ReferenceEquals( obj1, obj2 ) )

No need to involve pointers.

obj1=Activator.CreateInstance(t1); //Type t1; class A
obj2=Activator.CreateInstance(t2); //Type t2; class B

If you allocate two objects they will always be in different locations
in memory (with the exception of interned strings).



Mattias
 
Simple comparison like this won't work couse they can be similar only after
some offsetting... if ...

obj1.A.h=0
obj2.B.A.h=1;

I will need to bring them to ...

A.h=0 //obj1
A.h=1 //obj2

So ... still I will need the object pointer.
 
Why don't you just write a comparison method that compares the fields,
what's wrong with that? You could possibly implement IComparable for
this.



Mattias
 
Back
Top