Click here to Skip to main content
       

C#

 
QuestionBad design? PinmemberDaniel Sheets8-Nov-12 9:01 
AnswerRe: Bad design? PinmemberEddy Vluggen8-Nov-12 9:38 
GeneralRe: Bad design? PinmemberMatt T Heffron8-Nov-12 10:02 
GeneralRe: Bad design? PinmemberDaniel Sheets8-Nov-12 10:36 
AnswerRe: Bad design? PinmemberMarcus Kramer8-Nov-12 10:28 
AnswerRe: Bad design? PinmemberSledgeHammer018-Nov-12 11:24 
AnswerRe: Bad design? PinmemberClifford Nelson9-Nov-12 8:57 
GeneralRe: Bad design? PinmemberSheets.d9-Nov-12 9:04 
GeneralRe: Bad design? PinmemberClifford Nelson9-Nov-12 23:27 
GeneralMemory Stream PinmemberKushina8-Nov-12 8:04 
GeneralRe: Memory Stream PinmemberKushina8-Nov-12 8:05 
GeneralRe: Memory Stream Pinmemberjschell8-Nov-12 9:43 
GeneralRe: Memory Stream PinmemberKushina9-Nov-12 10:58 
QuestionTotal hack - but is it OK? [modified] Pinmemberharold aptroot8-Nov-12 5:58 
Say we have a struct A that looks like this:
struct A
{
    public string a0;
    public string a1;
    public string a2;
    public string a3;
 

    public unsafe string this[int index]
    {
        get { return *(&a0 + index); }
    }
}
Ok so you can't do that, it would only work for unmanaged types. Fixed size array then? Nope, also only works for unmanaged types.
 
So then I had a terrible idea:
struct A
{
    public string a0;
    public string a1;
    public string a2;
    public string a3;
 

    public unsafe string this[int index]
    {
        get { return get(ref this, index); }
    }
 
    delegate string Get(ref A obj, int index);
    static Get get;
 
    static A()
    {
        DynamicMethod dm = new DynamicMethod("getat", typeof(string), new Type[] { typeof(A).MakeByRefType(), typeof(int) }, typeof(A));
        ILGenerator gen = dm.GetILGenerator();
        gen.Emit(OpCodes.Ldarg_0);
        gen.Emit(OpCodes.Ldflda, typeof(A).GetField("a0"));
        gen.Emit(OpCodes.Sizeof, typeof(IntPtr));
        gen.Emit(OpCodes.Ldarg_1);
        gen.Emit(OpCodes.Mul);
        gen.Emit(OpCodes.Conv_I);
        gen.Emit(OpCodes.Add);
        gen.Emit(OpCodes.Ldind_Ref);
        gen.Emit(OpCodes.Ret);
        get = (Get)dm.CreateDelegate(typeof(Get));
    }
}
And guess what, it works (as far as I tested).
 
This is a total hack, obviously. But is it OK? Assuming it isn't given bad indexes, can it fail somehow?
 
I'm not really too interested in "how to do this the right way" (unless you've got something that isn't "just use an array" or "use a switch, it's only 4 items" (that's just for the example)), just in how terrible this way really is.

modified 8-Nov-12 12:15pm.

AnswerRe: Total hack - but is it OK? PinmvpDave Kreskowiak8-Nov-12 7:37 
AnswerRe: Total hack - but is it OK? PinmemberSledgeHammer018-Nov-12 7:43 
GeneralRe: Total hack - but is it OK? PinmemberBobJanova9-Nov-12 0:18 
AnswerRe: Total hack - but is it OK? PinmemberEddy Vluggen8-Nov-12 9:47 
GeneralRe: Total hack - but is it OK? Pinmemberharold aptroot8-Nov-12 10:05 
GeneralRe: Total hack - but is it OK? PinmemberEddy Vluggen8-Nov-12 10:24 
GeneralRe: Total hack - but is it OK? Pinmemberharold aptroot8-Nov-12 10:43 
GeneralRe: Total hack - but is it OK? PinmemberEddy Vluggen8-Nov-12 11:13 
AnswerDepends on your goal PinmemberEnnis Ray Lynch, Jr.9-Nov-12 8:26 
QuestionHow to get outlook mails using add-ins PinmemberSanjeev99188-Nov-12 0:10 
AnswerRe: How to get outlook mails using add-ins PinmvpDave Kreskowiak8-Nov-12 2:36 
GeneralRe: How to get outlook mails using add-ins PinmvpOriginalGriff8-Nov-12 5:15 
GeneralRe: How to get outlook mails using add-ins PinmemberSanjeev99189-Nov-12 0:31 
GeneralRe: How to get outlook mails using add-ins PinmvpDave Kreskowiak9-Nov-12 2:49 
QuestionConverting a stream of html to xml and reading the xml into a xmldocumet in c# PinmemberSteve Holdorf8-Nov-12 0:05 
AnswerRe: Converting a stream of html to xml and reading the xml into a xmldocumet in c# Pinmemberdeflinek8-Nov-12 0:20 
GeneralRe: Converting a stream of html to xml and reading the xml into a xmldocumet in c# PinmemberSteve Holdorf8-Nov-12 3:40 
AnswerRe: Converting a stream of html to xml and reading the xml into a xmldocumet in c# PinmemberRichard Deeming8-Nov-12 3:16 
GeneralRe: Converting a stream of html to xml and reading the xml into a xmldocumet in c# PinmemberSteve Holdorf10-Nov-12 2:07 
GeneralRe: Converting a stream of html to xml and reading the xml into a xmldocumet in c# PinmemberRichard Deeming12-Nov-12 2:58 
QuestionVSTO Outlook Visual C# - Create a ribbon that load a windows form integrated into outlook form? [modified] Pinmembercsharpcomeintome7-Nov-12 23:31 
QuestionHow to convert string to Datetime including milliseconds PinmemberVijay Kanda7-Nov-12 22:45 
AnswerRe: How to convert string to Datetime including milliseconds PinprotectorPete O'Hanlon7-Nov-12 23:18 
AnswerRe: How to convert string to Datetime including milliseconds PinmemberBMS1218-Nov-12 2:23 
GeneralRe: How to convert string to Datetime including milliseconds PinmemberVijay Kanda8-Nov-12 21:44 
GeneralRe: How to convert string to Datetime including milliseconds PinmemberBobJanova9-Nov-12 0:24 
GeneralRe: How to convert string to Datetime including milliseconds PinmemberVijay Kanda11-Nov-12 20:15 
GeneralRe: How to convert string to Datetime including milliseconds PinmemberBobJanova11-Nov-12 23:10 
Questionhow to sort Datalist using Dropdown which is not in the datalist control Pinmembersreerajsn1237-Nov-12 22:19 
Questiondatagridview child rows Pinmembermynamearan7-Nov-12 21:18 
AnswerRe: datagridview child rows PinmentorWayne Gaylard7-Nov-12 22:14 
GeneralRe: datagridview child rows PinmemberMycroft Holmes7-Nov-12 23:08 
GeneralRe: datagridview child rows PinmentorWayne Gaylard7-Nov-12 23:32 
QuestionC# linq to sql default problem Pinmembersc steinhayse7-Nov-12 13:57 
AnswerRe: C# linq to sql default problem PinmemberMycroft Holmes7-Nov-12 14:12 
GeneralRe: C# linq to sql default problem Pinmembersc steinhayse7-Nov-12 19:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 23 Nov 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid