Click here to Skip to main content
15,910,234 members
Home / Discussions / C#
   

C#

 
GeneralRe: I need help improving my code Pin
John Torjo5-Nov-15 19:45
professionalJohn Torjo5-Nov-15 19:45 
GeneralRe: I need help improving my code Pin
V.5-Nov-15 19:58
professionalV.5-Nov-15 19:58 
GeneralRe: I need help improving my code Pin
John Torjo5-Nov-15 20:04
professionalJohn Torjo5-Nov-15 20:04 
AnswerRe: I need help improving my code Pin
BillWoodruff5-Nov-15 19:25
professionalBillWoodruff5-Nov-15 19:25 
GeneralRe: I need help improving my code Pin
Gilbert Consellado5-Nov-15 23:42
professionalGilbert Consellado5-Nov-15 23:42 
GeneralRe: I need help improving my code Pin
BillWoodruff6-Nov-15 0:42
professionalBillWoodruff6-Nov-15 0:42 
GeneralRe: I need help improving my code Pin
Gilbert Consellado6-Nov-15 2:17
professionalGilbert Consellado6-Nov-15 2:17 
GeneralRe: I need help improving my code Pin
BillWoodruff6-Nov-15 2:40
professionalBillWoodruff6-Nov-15 2:40 
GeneralRe: I need help improving my code Pin
Gilbert Consellado6-Nov-15 3:00
professionalGilbert Consellado6-Nov-15 3:00 
GeneralRe: I need help improving my code Pin
BillWoodruff6-Nov-15 4:14
professionalBillWoodruff6-Nov-15 4:14 
GeneralRe: I need help improving my code Pin
Gilbert Consellado6-Nov-15 4:45
professionalGilbert Consellado6-Nov-15 4:45 
AnswerRe: I need help improving my code Pin
V.5-Nov-15 19:52
professionalV.5-Nov-15 19:52 
GeneralRe: I need help improving my code Pin
Gilbert Consellado6-Nov-15 0:04
professionalGilbert Consellado6-Nov-15 0:04 
AnswerRe: I need help improving my code Pin
Chris Quinn5-Nov-15 22:14
Chris Quinn5-Nov-15 22:14 
GeneralRe: I need help improving my code Pin
Gilbert Consellado6-Nov-15 0:08
professionalGilbert Consellado6-Nov-15 0:08 
AnswerRe: I need help improving my code Pin
Rob Philpott5-Nov-15 23:03
Rob Philpott5-Nov-15 23:03 
GeneralRe: I need help improving my code Pin
Gilbert Consellado6-Nov-15 0:12
professionalGilbert Consellado6-Nov-15 0:12 
Questionwhy you cannot "spoof" an Interface to use to cast an Object to a Type you know the structure of ? Pin
BillWoodruff4-Nov-15 23:25
professionalBillWoodruff4-Nov-15 23:25 
AnswerRe: why you cannot "spoof" an Interface to use to cast an Object to a Type you know the structure of ? Pin
Eddy Vluggen4-Nov-15 23:42
professionalEddy Vluggen4-Nov-15 23:42 
GeneralRe: why you cannot "spoof" an Interface to use to cast an Object to a Type you know the structure of ? Pin
BillWoodruff5-Nov-15 4:44
professionalBillWoodruff5-Nov-15 4:44 
GeneralRe: why you cannot "spoof" an Interface to use to cast an Object to a Type you know the structure of ? Pin
Eddy Vluggen5-Nov-15 8:28
professionalEddy Vluggen5-Nov-15 8:28 
AnswerRe: why you cannot "spoof" an Interface to use to cast an Object to a Type you know the structure of ? PinPopular
Pete O'Hanlon5-Nov-15 0:55
mvePete O'Hanlon5-Nov-15 0:55 
The answer, as always, lies in the il. Specifically, you're looking at these two lines:
L_001a: ldfld class SpoofTest.ExampleClass SpoofTest.Class1::example
L_001f: isinst SpoofTest.IExample
So, the code loads the instance and then tests it to see if it's an instance of your interface. Obviously, it's not an instance so the code would leave iexample as nulL, so this code
C#
public Class1()
{
  IExample iexample = example as IExample;
  if (iexample == null)
  {
    System.Diagnostics.Debug.WriteLine("This does not compute");
  }
}
produces this IL
.method public hidebysig specialname rtspecialname instance void .ctor() cil managed
{
    .maxstack 3
    .locals init (
        [0] class SpoofTest.IExample iexample,
        [1] bool CS$4$0000)
    L_0000: ldarg.0
    L_0001: ldc.i4.1
    L_0002: ldstr "Test"
    L_0007: newobj instance void SpoofTest.ExampleClass::.ctor(int32, string)
    L_000c: stfld class SpoofTest.ExampleClass SpoofTest.Class1::example
    L_0011: ldarg.0
    L_0012: call instance void [mscorlib]System.Object::.ctor()
    L_0017: nop
    L_0018: nop
    L_0019: ldarg.0
    L_001a: ldfld class SpoofTest.ExampleClass SpoofTest.Class1::example
    L_001f: isinst SpoofTest.IExample
    L_0024: stloc.0
    L_0025: ldloc.0
    L_0026: ldnull
    L_0027: ceq
    L_0029: ldc.i4.0
    L_002a: ceq
    L_002c: stloc.1
    L_002d: ldloc.1
    L_002e: brtrue.s L_003d
    L_0030: nop
    L_0031: ldstr "This does not compute"
    L_0036: call void [System]System.Diagnostics.Debug::WriteLine(string)
    L_003b: nop
    L_003c: nop
    L_003d: nop
    L_003e: ret
}
Now, compare this to what happens if I change ExampleClass to implement IExample. The IL changes to this
.method public hidebysig specialname rtspecialname instance void .ctor() cil managed
{
    .maxstack 3
    .locals init (
        [0] class SpoofTest.IExample iexample,
        [1] bool CS$4$0000)
    L_0000: ldarg.0
    L_0001: ldc.i4.1
    L_0002: ldstr "Test"
    L_0007: newobj instance void SpoofTest.ExampleClass::.ctor(int32, string)
    L_000c: stfld class SpoofTest.ExampleClass SpoofTest.Class1::example
    L_0011: ldarg.0
    L_0012: call instance void [mscorlib]System.Object::.ctor()
    L_0017: nop
    L_0018: nop
    L_0019: ldarg.0
    L_001a: ldfld class SpoofTest.ExampleClass SpoofTest.Class1::example
    L_001f: stloc.0
    L_0020: ldloc.0
    L_0021: ldnull
    L_0022: ceq
    L_0024: ldc.i4.0
    L_0025: ceq
    L_0027: stloc.1
    L_0028: ldloc.1
    L_0029: brtrue.s L_0038
    L_002b: nop
    L_002c: ldstr "This does not compute"
    L_0031: call void [System]System.Diagnostics.Debug::WriteLine(string)
    L_0036: nop
    L_0037: nop
    L_0038: nop
    L_0039: ret
}
You'll obviously notice, at this stage, that the isinst check is no longer there.
GeneralRe: why you cannot "spoof" an Interface to use to cast an Object to a Type you know the structure of ? Pin
BillWoodruff5-Nov-15 4:46
professionalBillWoodruff5-Nov-15 4:46 
GeneralRe: why you cannot "spoof" an Interface to use to cast an Object to a Type you know the structure of ? Pin
Pete O'Hanlon5-Nov-15 5:07
mvePete O'Hanlon5-Nov-15 5:07 
QuestionOAUTH2 Access from .NET Console/WinForms Pin
Member 119360464-Nov-15 22:43
Member 119360464-Nov-15 22:43 

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

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