Re: Late Construction via Reflection & instance variable initializers
From: Robert Jordan (robertj_at_gmx.net)
Date: 09/11/04
- Next message: Robert Jordan: "Re: Late Construction via Reflection & instance variable initializers"
- Previous message: Slava M. Usov: "Re: How to retrieve serial number of OS or CPU for copy protection?"
- In reply to: Robert Hooker: "Re: Late Construction via Reflection & instance variable initializers"
- Next in thread: Robert Jordan: "Re: Late Construction via Reflection & instance variable initializers"
- Messages sorted by: [ date ] [ thread ]
Date: Sat, 11 Sep 2004 23:34:25 +0200
Robert Hooker wrote:
> Richard and Robert,
>
> Thanks for your replies (on a saturday!).
>
> The following seems to work - though I'm vaguely uneasy about it. Its a
> combo of both of your replies (but without resorting to IL)
cool! ;-)
bye
Rob
>
> Wibble w = new Wibble()
> // stuff occurs to alter the state of wibble
> ;
>
> //This sets everything to 'zero' on a new Wibblw instance - but doesn't call
> any ctors (so I assume its fairly cheap)
> object o = FormatterServices.GetUninitializedObject(typeof(wibble);
>
> //Copy the 'zero's' across to my Wibble instance
> foreach (FieldInfo fi in WibblesFields)
> {
> fo.SetValue(w, fi.GetValue(o));
> }
>
> //Then invoke the ctor of wibble - this sets up all values which are 'zero'
> ci = GetConstructor();
> co.Invoke(w,null)
>
>
> My uneasiness comes from the fact thatf or most fields I'll be setting them
> to zero, then the ctor will assign the real value - kind of wasteful...
>
>
>
>
> "Richard Blewett [DevelopMentor]" <richardb@develop.com> wrote in message
> news:ueKjl3CmEHA.596@TK2MSFTNGP11.phx.gbl...
>
>>The only problem with this approach (though at the moment I don't have a
>
> massive alternative without resorting to IL) is that :
>
>> 1) you need to know how the initialization of Wibble actually works.
>
> It probably isn't as simple as zeroing out the fields
>
>> 2) You need to be running with a high level of trust to be able to
>
> be able to reflect on private members – CAS may be your undoing.
>
>> To be honest, unless you are going to resort to IL (pass in the objref to
>
> a method and call initobj and then the ctor in that method) then I don't
> think you have a foolproof way of doing this) – and without testing
> I'm not sure that this approach would be verifiable (although I can't think
> of why it wouldn't be) and if it isn't you may still fall foul of CAS.
>
>> Regards
>>
>> Richard Blewett – DevelopMentor
>>
>> http://staff.develop.com/richardb/weblog
>>
>>
>
> nntp://news.microsoft.com/microsoft.public.dotnet.framework.clr/
>
>> >
>> > How about this:
>> >
>> > - create a new Wibble instance
>> > - reflect on the new instance *andÜ on the instance you want
>> > to reset and set the fields to the values of the
>> > new instance.
>>
>> pseudo code:
>>
>> static void ResetWibble(Wibble old) {
>> Wibble w = new Wibble();
>>
>> FieldInfo[] newFields = w.GetType.GetFields(...);
>> FieldInfo[] oldFields = old.GetType.GetFields(...);
>>
>> for (int i = 0; i < newFields.Length; i++) {
>> newFields[i].SetValue(old, oldFields[i].GetValue(w));
>> }
>>
>> }
>>
>> bye
>> Rob
>>
>>
>>
>> ---
>> Incoming mail is certified Virus Free.
>> Checked by AVG anti-virus system (http://www.grisoft.com).
>> Version: 6.0.759 / Virus Database: 508 - Release Date: 09/09/2004
>>
>>
>> [microsoft.public.dotnet.framework.clr]
>>
>>
>
>
>
- Next message: Robert Jordan: "Re: Late Construction via Reflection & instance variable initializers"
- Previous message: Slava M. Usov: "Re: How to retrieve serial number of OS or CPU for copy protection?"
- In reply to: Robert Hooker: "Re: Late Construction via Reflection & instance variable initializers"
- Next in thread: Robert Jordan: "Re: Late Construction via Reflection & instance variable initializers"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|