How can I capture the name of a C# static string resource property without changing the caller?
<p>Having string resources in Strings.resx/Strings.Designer.cs, used like this:</p>

<pre><code>myObject.DoSomething(Strings.HelloWorld);
</code></pre>

<p>can I capture the resource id (i.e. "Strings.HelloWorld") in DoSomething without changing the source line that calls DoSomething? I want the name of the static property defined in Strings.Designer.cs, along with the string value.</p>

<p>What I've figured out is:</p>

<pre><code>myObject.DoSomething(() =&gt; Strings.HelloWorld);
</code></pre>

<p>and then:</p>

<pre><code>void DoSomething(Expression&lt;Func&lt;string&gt;&gt; expr)
{
    string id = expr.Body.ToString();
    string value = (expr.Compile())();
    // do something with these...
}
</code></pre>

<p>That changes the original line slightly, but it's still readable and the compiler is checking the property name for me.</p>

<p><strong>I was wondering if there is any C# magic to do this without changing that original line.</strong></p>

<p>EDIT: Really, I just want unique ints for each string resource that I use to format a TraceEvent message, to use as the TraceEvent id. I don't want to maintain a list of ints myself when the compiler can do that for me, adding one every time I insert a new source line to trace something else interesting. I get my ints once in initialization by mapping the ResourceSet keys to a sequence of ints, and I don't mind if the numbers shift around from build to build.</p>

<p>EDIT2: Referring to LBushkin's answer, I did initially map strings to int, like this:</p>

<pre><code>var resourceSet = resourceManager.GetResourceSet(CultureInfo.CurrentCulture, true, true);

var sorted =
    from i in resourceSet.Cast&lt;DictionaryEntry&gt;()
    orderby i.Key
    select i.Value as string;

var valueToId = new Dictionary&lt;string, int&gt;();

foreach (string value in sorted)
{
    valueToId[value] = valueToId.Count + 100; // whatever range you like
}
</code></pre>

<p>...but I don't like relying on a static CurrentCulture. I want a simple low-tech, low-maintenance mapping of the resource strings to unique ints, where all I need to do is add strings in the designer when I feel like it.</p>
