May be you can use MemberInfo/FieldInfo and join the Name(which will give
you the filed name) and DeclaringType.Name/FullName to get the
representation the way you expected. For eg,
FieldInfo memInfo = obj.GetType().GetField("MyString1");
string exp = memInfo.DeclaringType.Name + "." + memInfo.Name;
will give you something like this : ClassName.FieldName.
I dont think there is a streight way exist which will give the output the
way you expect.
Thanks
Siyad
On Thu, Feb 19, 2009 at 11:11 PM, Michael Brandt Lassen <
[email protected]> wrote:
>
> Hi Nikhil
>
> Thank you for your reply. Unfortunately your suggestion doesn't work.
> Since Member1 is a member variable and not a nested type
> ( myClass.Member1.GetType() ).DeclaringType is null resulting in a
> NullReference exception.
>
> Any other suggestions?
>
> Michael Brandt Lassen
>
>
> On 18 Feb., 15:57, nikhil gaitonde <[email protected]> wrote:
> > Hi Michael,
> >
> > try the following code
> >
> > ( myClass.Member1.GetType() ).DeclaringType
> >
> > You will get other properties as well like the assembly.
> > Just try it and let me know if you need anything else.
> >
> > Regards,
> > Nikhil
> >
> > On Wed, Feb 18, 2009 at 8:13 PM, Michael Brandt Lassen <
> >
> >
> >
> > [email protected]> wrote:
> >
> > > Hi there gurus.
> >
> > > Is it possible using reflection to get the name of the surrounding
> > > class name from a member (field)?
> >
> > > Say, I have a class like:
> >
> > > private class MyClass
> > > {
> > > public string Member1;
> > > }
> >
> > > Let's make an instance:
> >
> > > MyClass myClass = new MyClass();
> > > myClass.Member1 = "member1";
> >
> > > Now, is it possible to reflect on myClass.Member1 to return the string
> > > "MyClass.Member1"?
> >
> > > Or for the TDD guys: Can you make this test pass:
> >
> > > [TestMethod]
> > > public void TestMethod1()
> > > {
> > > MyClass myClass = new MyClass();
> > > myClass.Member1 = "member1";
> >
> > > string expected = "MyClass.Member1";
> > > string actual="";
> >
> > > //actual = MagicalReflectionOn on myClass.Member1
> >
> > > Assert.AreEqual(expected, actual);
> > > }
> >
> > > Best regards
> >
> > > Michael Brandt Lassen- Skjul tekst i anførselstegn -
> >
> > - Vis tekst i anførselstegn -
>