gsobocinski
gsobocinski

Reputation: 295

C# Reflection: Emit DateTime Property Value

I am currently working with generating dynamic types using Reflection.Emit.

I have the majority of what I need working however need to set default property values in the constructor.

I have so far got this working for Integers and Strings by using for example (reduced for brevity):

ctorDefaultIL.Emit(OpCodes.Ldstr, "Hello World");

ctorDefaultIL.Emit(OpCodes.Ldc_I4, 42);

This works perfectly, my question is how would I go about setting a DateTime value.

Upvotes: 2

Views: 618

Answers (1)

gsobocinski
gsobocinski

Reputation: 295

Thanks @leppie for future ref here's the code I ended up using. For some context this is being passed into another method:

ctorDefaultIL.Emit(OpCodes.Ldc_I8, dateVal.Ticks);
ctorDefaultIL.Emit(OpCodes.Newobj, 
  typeof(DateTime).GetConstructor(new[] { typeof(long) }));

Upvotes: 3

Related Questions