locked
get const reference from assembly RRS feed

  • Question

  • hi, i don't think that is psssible but i try to asky...

    if i have a class

    class Class1
    {
        public const string message = "Hello";

        public string Message1  {
            get { return message; }
         }

        public string Message2  {
            get { return "goodbye"; }
         }
    }

    i want know if there's a way to know from the compiled assembly if a returned string is taken by a const (and what const is used) or if it's an inline string.

    i can't modify the original assembly

    thanks



    Friday, February 29, 2008 1:09 PM

Answers

  • You are asking to find the the solution to 1-1 > 0. Two const strings in one or several assemblies will point to the same const object reference. When an assembly is loaded all const strings are put into an internal cross AppDomain string pool to save memory. So in effect the actual object you will get does not live in one or the other assembly it comes from the global const string pool. For more infos have a look at the String.Intern method. You would need to track the function calls to validate that a metods from another assembly was called. It is possible by using the profiling APIs but the performance penalty is way to high. Could you describe what you are trying to achieve in general? This "solution" is not something I would recommend to my worst enemy ;-).

    Yours,
      Alois Kraus

    Sunday, March 2, 2008 11:09 PM

All replies

  • You could always compare the property string to the const string and see if they are equal.  You could even use reflection to parse out the method body of the property to see where it is coming from.  So the answer is probably yes, but not very cleanly.
    Friday, February 29, 2008 1:30 PM
  • i have write a bad example, the correct is

    class1 {
     
      public string Message1 {
           return someOtherClass.MessageConst;
     }

      public string Message1 {        // 2 version
           return "inlineMessage"
      }
    }


     i want know if from assembly i can distinguish this 2 case, and if it's the first if i can get the source const.


     
    Friday, February 29, 2008 1:54 PM
  • You could do it like this:

    Code Snippet

    System.Reflection.PropertyInfo pi = Type.GetType("class1").GetProperty("Message1");
    System.Reflection.MethodBody mb = pi.GetGetMethod().GetMethodBody();
    mb.GetILAsByteArray();



    Then check the byte array of the IL and see if it is contians that const string.
    Friday, February 29, 2008 1:59 PM
  • my problem is that i don't know if a const exists, if i see the IL i only see something as

    public string Message
    {
          get  {retunr ="hello";}
    }

    but i don't know if that string is an inline string or if is taken from a const.

    So i want know if there's a way to know if a const is used for get that value and where the const is defined.

    thanks
         
    Friday, February 29, 2008 4:31 PM
  • Are you coding this yourself?  If so you could create a field that contains a boolean if this uses a const.  Or better yet create a custom attribute you can use to tag your properies with such as:

    [UseConstAttribute(true)]
    public string Message
    {
          get  {retunr ="hello";}
    }

    Then you can just use reflection to see if the property inplements the attribute and if the value is true.
    Friday, February 29, 2008 4:34 PM
  • i have a third party assembly without code
    Friday, February 29, 2008 4:47 PM
  • You are asking to find the the solution to 1-1 > 0. Two const strings in one or several assemblies will point to the same const object reference. When an assembly is loaded all const strings are put into an internal cross AppDomain string pool to save memory. So in effect the actual object you will get does not live in one or the other assembly it comes from the global const string pool. For more infos have a look at the String.Intern method. You would need to track the function calls to validate that a metods from another assembly was called. It is possible by using the profiling APIs but the performance penalty is way to high. Could you describe what you are trying to achieve in general? This "solution" is not something I would recommend to my worst enemy ;-).

    Yours,
      Alois Kraus

    Sunday, March 2, 2008 11:09 PM
  • This is true, i have been struggling with why you would want to get the const.   The non-const string is still the const string.  Strings are imutable objects so both properties are pointing to the same object in memory so they are both the exact same thing.  Is there some other compelling reason why you need the const?  If so we might be able to suggest a work around.

     

    Sunday, March 2, 2008 11:16 PM
  • my problem is that, i'm using a third party assembly with a method wich with dissasembly appear as

    string ToString()
    {
    return ToString("custom format label");
    }

    i need to use the same string into my code, so i want know if i can reference some public const or if i must declare my own const with the same value, i prefer the first solution becouse if the other assembly changes const value, i can directly recompile my dll to use the const with new value. my problem is that i don't know if a  const is used.

    (also knowing if is taken by a private/internal const is useful)
    Tuesday, March 4, 2008 4:11 PM
  • Hi,

    I do not think it is possible to retrieve the value of a const string which is not stored as a member of a class. Since you do not own the library I suspect it will not change very often. So you can decide when to upgrade. I would recommend some unit tests to mitigate this problem to validate that your format string and the external dll do provide the same output. From a best practice point of view this would be the best solution to mitigate any (or other) breaking changes of your external assembly.

    Yours,
      Alois Kraus

    Tuesday, March 4, 2008 9:35 PM