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 -

Reply via email to