2

Class X uses the reflection methods to check whether Class Y has a particular method. Is there any way for Class Y to find out the details of the method Class X was checking for?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • 3
    Um. Class Y is should be "aware" of its own methods. What exactly are you asking here? – spender Mar 11 '13 at 14:32
  • 1
    Sounds like too much reflection. Reflection on reflection. Please explain what you need. – Federico Berasategui Mar 11 '13 at 14:33
  • In a word: No. [15 chars] – Matthew Watson Mar 11 '13 at 14:33
  • 3
    My money is on an [XY Problem](http://www.perlmonks.org/index.pl?node_id=542341) – Austin Salonen Mar 11 '13 at 14:34
  • I rhink so it couldn't possible – Smaug Mar 11 '13 at 14:34
  • 1
    It sounds like your overcomplicating the solution (to whatever your issue is) in your head. Time to take a step back, grab a cup of tea and re-evaluate. – Amicable Mar 11 '13 at 14:36
  • 1
    **Theory:** The OP is trying to write a library which is Reflection-proof, by bombing out if you try to Reflect it. – Bobson Mar 11 '13 at 14:39
  • It sounds like you are looking for some kind of obfuscation solution. Can you explain what you are trying to do or prevent? – Brendan Hannemann Mar 11 '13 at 14:41
  • I have another class that makes undocumented calls on my class and I'm trying find how what all of those calls are so I can put my code in the appropriate methods rather than using those I know about and extra checks. I'm not allowed to decompile. – EndlessWaves Mar 11 '13 at 15:03
  • 1
    @EndlessWaves - If another class is making undocumented calls to your class, rather than only using the things you've declared `public`, then either 1) You didn't expose functionality which you should have, or 2) It's the coder of that class' responsibility to fix it when it breaks after you make changes. You shouldn't be responsible for maintaining code which uses yours in an unsupported manner. – Bobson Mar 11 '13 at 15:10

2 Answers2

5

Reflection is performed on the Type definition not an instance.

The instance is used to retrieve values if required and is only the storage location of the values, the Type definition is the map of this storage area.

Therefor it is not possible to indicate if Reflection has been used on an instance... It is possible to determine if Reflection has been performed on a Type recently (before the last garbage collector run, think reflecting Reflection) however that tangent is best not explored further here because it still does not solve the problem you set out to achieve.

When the CLR performs reflection it does so in way which does not leave any traces that reflection has been performed save the memory costs associated with performing the reflection.

It would not be possible to do leave any such indication without modifying the CLR.

Another way to solve this problem is to provide a reflection cache which stores the type information in a Dictionary, this will allow you to determine if you have reflected a specific type or not and then use the information in the Dictionary rather then performing reflection again.

See How do I intercept a method call in C#?

Community
  • 1
  • 1
Jay
  • 3,276
  • 1
  • 28
  • 38
2

The only way class Y can know that class X is reflecting it is if class X somehow let class Y know by invoking a method or setting or getting the value of a property. So the straight answer is No.

alejosoft
  • 186
  • 2
  • 7
  • 1
    Correct, In the same notion one would be able to create an intentionally unused property and then flag a private variable is the property getter or setter was invoked. The problem with this is that reflection in general without the getting and setting of the values will still have no effect on the flag and thus leave no trace. – Jay Mar 11 '13 at 14:46