Click here to Skip to main content
15,886,919 members
Home / Discussions / C#
   

C#

 
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 
A question on QA [^] from someone who knows the internal structure of a Type, but does not have a reference to the Type or an Interface the Type implements ... and who needs to cast the Type as Object to its original Type to access its internal field.

In other words: I have an instance of this Class cast to Object:
C#
public class ExampleClass
{
    public int ExId { set; get; }

    public string ExString { set; get; }

    public ExampleClass(int exId, string exString)
    {
        ExId = exId;
        ExString = exString;
    }
}
And, my code does not contain the definition of this Class.

So I want to cast the instance of ExampleClass as Object to its Native Type. The thought occurred to me that it might be possible, since I know the internal structure of ExampleClass to "spoof" an Interface like this:
C#
public interface IExample
{
    int ExId { set; get; }

    string ExString { set; get; }
}
And, use it like this on the instance to get it back to ExampleClass Type:

// assume I have an instance of ExampleClass cast to Object named 'example<br />
IExample iexample = example as IExample;


This results in the value of 'iexample being set to 'null.

I also tried "spoofing" the Class, and using that:

C#
public class xExample
{
    public int ExId { set; get; }

    public string ExString { set; get; }

    public xExample(int exId, string exString)
    {
        ExId = exId;
        ExString = exString;
    }
}

// results in 'null
xExample xexample = ExampleClassAsObject as (xExample);
My conclusion is that this kind of "spoofing" is not possible in .NET, and, being foolish enough to ask why, I ask: why ? Security reasons ? A "spoof" is never "the real thing" ?

Just curious, thanks.
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.

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 
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 
AnswerRe: OAUTH2 Access from .NET Console/WinForms Pin
Dave Kreskowiak5-Nov-15 6:23
mveDave Kreskowiak5-Nov-15 6:23 
Questionevery email is ok,even email address is like that(sdfsdfsdf@gmail.com ) Pin
Member 118929084-Nov-15 20:32
Member 118929084-Nov-15 20:32 
AnswerRe: every email is ok,even email address is like that(sdfsdfsdf@gmail.com ) Pin
John Torjo4-Nov-15 20:52
professionalJohn Torjo4-Nov-15 20:52 
AnswerRe: every email is ok,even email address is like that(sdfsdfsdf@gmail.com ) Pin
OriginalGriff4-Nov-15 21:10
mveOriginalGriff4-Nov-15 21:10 
QuestionColumn Reorder winform Pin
Member 118527314-Nov-15 2:32
Member 118527314-Nov-15 2:32 
AnswerRe: Column Reorder winform Pin
BillWoodruff4-Nov-15 18:47
professionalBillWoodruff4-Nov-15 18:47 
GeneralRe: Column Reorder winform Pin
Member 118527315-Nov-15 0:32
Member 118527315-Nov-15 0:32 
GeneralRe: Column Reorder winform Pin
BillWoodruff5-Nov-15 4:42
professionalBillWoodruff5-Nov-15 4:42 
GeneralRe: Column Reorder winform Pin
Member 118527316-Nov-15 4:01
Member 118527316-Nov-15 4:01 
GeneralRe: Column Reorder winform Pin
BillWoodruff10-Nov-15 14:24
professionalBillWoodruff10-Nov-15 14:24 

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.