Hi Yves,

The InitialValue is a particular beast for primitive types.

I encourage you to look at the IL created for your C# code in ildasm,
you'll get a class constructor initializing the field with a string,
like this:

MyClass::.cctor
{
ldstr "The Field Value"
stsfld MyClass.MyString
}

You just need to recreate this.

Jb

On Fri, Mar 14, 2014 at 11:12 PM, Yves Goergen <[email protected]> wrote:
> I'd like to create a new field in a class and it should have an initial
> value. I'm thinking of something like this C# code:
>
> class MyClass
> {
>     public static string MyString = "The field value";
> }
>
> I have the following code right now:
>
> var fieldDef = new FieldDefinition(
>     "MyString",
>     FieldAttributes.Public | FieldAttributes.Static,
>     MyTypeDef.Module.Import(typeof(string)));
> fieldDef.InitialValue = Encoding.Unicode.GetBytes("The field value");
> MyTypeDef.Fields.Add(fieldDef);
>
> The Unicode encoding thing is a wild guess to convert a string to a byte
> array. But I have no clue what to do here. The current result is that the
> field is created but has no value. Reading the Cecil source code in this
> part doesn't help me. Is there any documentation about how to use this? I
> haven't found any documentation about how to use anything in Cecil yet, I've
> come surprisingly far by just trying things out, guessing a lot and googling
> the rest...
>
> --
> --
> --
> mono-cecil
> ---
> You received this message because you are subscribed to the Google Groups
> "mono-cecil" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.

-- 
-- 
--
mono-cecil
--- 
You received this message because you are subscribed to the Google Groups 
"mono-cecil" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to