Click here to Skip to main content
11,720,364 members (70,020 online)
   

C#

 
QuestionBad design? Pin
Daniel Sheets8-Nov-12 8:01
memberDaniel Sheets8-Nov-12 8:01 
AnswerRe: Bad design? Pin
Eddy Vluggen8-Nov-12 8:38
memberEddy Vluggen8-Nov-12 8:38 
GeneralRe: Bad design? Pin
Matt T Heffron8-Nov-12 9:02
memberMatt T Heffron8-Nov-12 9:02 
GeneralRe: Bad design? Pin
Daniel Sheets8-Nov-12 9:36
memberDaniel Sheets8-Nov-12 9:36 
AnswerRe: Bad design? Pin
Marcus Kramer8-Nov-12 9:28
memberMarcus Kramer8-Nov-12 9:28 
AnswerRe: Bad design? Pin
SledgeHammer018-Nov-12 10:24
memberSledgeHammer018-Nov-12 10:24 
AnswerRe: Bad design? Pin
Clifford Nelson9-Nov-12 7:57
memberClifford Nelson9-Nov-12 7:57 
GeneralRe: Bad design? Pin
Sheets.d9-Nov-12 8:04
memberSheets.d9-Nov-12 8:04 
GeneralRe: Bad design? Pin
Clifford Nelson9-Nov-12 22:27
memberClifford Nelson9-Nov-12 22:27 
GeneralMemory Stream Pin
Kushina8-Nov-12 7:04
memberKushina8-Nov-12 7:04 
GeneralRe: Memory Stream Pin
Kushina8-Nov-12 7:05
memberKushina8-Nov-12 7:05 
GeneralRe: Memory Stream Pin
jschell8-Nov-12 8:43
memberjschell8-Nov-12 8:43 
GeneralRe: Memory Stream Pin
Kushina9-Nov-12 9:58
memberKushina9-Nov-12 9:58 
QuestionTotal hack - but is it OK? Pin
harold aptroot8-Nov-12 4:58
memberharold aptroot8-Nov-12 4: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? Pin
Dave Kreskowiak8-Nov-12 6:37
mvpDave Kreskowiak8-Nov-12 6:37 
AnswerRe: Total hack - but is it OK? Pin
SledgeHammer018-Nov-12 6:43
memberSledgeHammer018-Nov-12 6:43 
GeneralRe: Total hack - but is it OK? Pin
BobJanova8-Nov-12 23:18
memberBobJanova8-Nov-12 23:18 
AnswerRe: Total hack - but is it OK? Pin
Eddy Vluggen8-Nov-12 8:47
memberEddy Vluggen8-Nov-12 8:47 
GeneralRe: Total hack - but is it OK? Pin
harold aptroot8-Nov-12 9:05
memberharold aptroot8-Nov-12 9:05 
GeneralRe: Total hack - but is it OK? Pin
Eddy Vluggen8-Nov-12 9:24
memberEddy Vluggen8-Nov-12 9:24 
GeneralRe: Total hack - but is it OK? Pin
harold aptroot8-Nov-12 9:43
memberharold aptroot8-Nov-12 9:43 
GeneralRe: Total hack - but is it OK? Pin
Eddy Vluggen8-Nov-12 10:13
memberEddy Vluggen8-Nov-12 10:13 
AnswerDepends on your goal Pin
Ennis Ray Lynch, Jr.9-Nov-12 7:26
memberEnnis Ray Lynch, Jr.9-Nov-12 7:26 
QuestionHow to get outlook mails using add-ins Pin
Sanjeev99187-Nov-12 23:10
memberSanjeev99187-Nov-12 23:10 
AnswerRe: How to get outlook mails using add-ins Pin
Dave Kreskowiak8-Nov-12 1:36
mvpDave Kreskowiak8-Nov-12 1:36 
GeneralRe: How to get outlook mails using add-ins Pin
OriginalGriff8-Nov-12 4:15
mvpOriginalGriff8-Nov-12 4:15 
GeneralRe: How to get outlook mails using add-ins Pin
Sanjeev99188-Nov-12 23:31
memberSanjeev99188-Nov-12 23:31 
GeneralRe: How to get outlook mails using add-ins Pin
Dave Kreskowiak9-Nov-12 1:49
mvpDave Kreskowiak9-Nov-12 1:49 
QuestionConverting a stream of html to xml and reading the xml into a xmldocumet in c# Pin
Steve Holdorf7-Nov-12 23:05
memberSteve Holdorf7-Nov-12 23:05 
AnswerRe: Converting a stream of html to xml and reading the xml into a xmldocumet in c# Pin
deflinek7-Nov-12 23:20
memberdeflinek7-Nov-12 23:20 
GeneralRe: Converting a stream of html to xml and reading the xml into a xmldocumet in c# Pin
Steve Holdorf8-Nov-12 2:40
memberSteve Holdorf8-Nov-12 2:40 
AnswerRe: Converting a stream of html to xml and reading the xml into a xmldocumet in c# Pin
Richard Deeming8-Nov-12 2:16
memberRichard Deeming8-Nov-12 2:16 
GeneralRe: Converting a stream of html to xml and reading the xml into a xmldocumet in c# Pin
Steve Holdorf10-Nov-12 1:07
memberSteve Holdorf10-Nov-12 1:07 
GeneralRe: Converting a stream of html to xml and reading the xml into a xmldocumet in c# Pin
Richard Deeming12-Nov-12 1:58
memberRichard Deeming12-Nov-12 1:58 
QuestionVSTO Outlook Visual C# - Create a ribbon that load a windows form integrated into outlook form? Pin
csharpcomeintome7-Nov-12 22:31
membercsharpcomeintome7-Nov-12 22:31 
QuestionHow to convert string to Datetime including milliseconds Pin
Vijay Kanda7-Nov-12 21:45
memberVijay Kanda7-Nov-12 21:45 
AnswerRe: How to convert string to Datetime including milliseconds Pin
Pete O'Hanlon7-Nov-12 22:18
protectorPete O'Hanlon7-Nov-12 22:18 
AnswerRe: How to convert string to Datetime including milliseconds Pin
BMS1218-Nov-12 1:23
memberBMS1218-Nov-12 1:23 
GeneralRe: How to convert string to Datetime including milliseconds Pin
Vijay Kanda8-Nov-12 20:44
memberVijay Kanda8-Nov-12 20:44 
GeneralRe: How to convert string to Datetime including milliseconds Pin
BobJanova8-Nov-12 23:24
memberBobJanova8-Nov-12 23:24 
GeneralRe: How to convert string to Datetime including milliseconds Pin
Vijay Kanda11-Nov-12 19:15
memberVijay Kanda11-Nov-12 19:15 
GeneralRe: How to convert string to Datetime including milliseconds Pin
BobJanova11-Nov-12 22:10
memberBobJanova11-Nov-12 22:10 
Questionhow to sort Datalist using Dropdown which is not in the datalist control Pin
sreerajsn1237-Nov-12 21:19
membersreerajsn1237-Nov-12 21:19 
Questiondatagridview child rows Pin
mynamearan7-Nov-12 20:18
membermynamearan7-Nov-12 20:18 
AnswerRe: datagridview child rows Pin
Wayne Gaylard7-Nov-12 21:14
mentorWayne Gaylard7-Nov-12 21:14 
GeneralRe: datagridview child rows Pin
Mycroft Holmes7-Nov-12 22:08
memberMycroft Holmes7-Nov-12 22:08 
GeneralRe: datagridview child rows Pin
Wayne Gaylard7-Nov-12 22:32
mentorWayne Gaylard7-Nov-12 22:32 
QuestionC# linq to sql default problem Pin
sc steinhayse7-Nov-12 12:57
membersc steinhayse7-Nov-12 12:57 
AnswerRe: C# linq to sql default problem Pin
Mycroft Holmes7-Nov-12 13:12
memberMycroft Holmes7-Nov-12 13:12 
GeneralRe: C# linq to sql default problem Pin
sc steinhayse7-Nov-12 18:14
membersc steinhayse7-Nov-12 18: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.150901.1 | Last Updated 4 Sep 2015
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid