Click here to Skip to main content
15,884,353 members
Home / Discussions / C#
   

C#

 
GeneralRe: Can the recall frmLogIn ? Pin
Member 24584678-Jul-14 20:49
Member 24584678-Jul-14 20:49 
QuestionTrying to get List<T> through reflection Pin
joost.versteegen28-Jun-14 5:29
joost.versteegen28-Jun-14 5:29 
AnswerRe: Trying to get List<T> through reflection Pin
Alan N28-Jun-14 6:12
Alan N28-Jun-14 6:12 
GeneralRe: Trying to get List<T> through reflection Pin
joost.versteegen28-Jun-14 23:20
joost.versteegen28-Jun-14 23:20 
AnswerRe: Trying to get List<T> through reflection Pin
Alan N29-Jun-14 0:55
Alan N29-Jun-14 0:55 
GeneralRe: Trying to get List<T> through reflection Pin
joost.versteegen29-Jun-14 1:12
joost.versteegen29-Jun-14 1:12 
AnswerRe: Trying to get List<T> through reflection Pin
PIEBALDconsult28-Jun-14 7:48
mvePIEBALDconsult28-Jun-14 7:48 
AnswerRe: Trying to get List<T> through reflection Pin
Richard Deeming30-Jun-14 1:47
mveRichard Deeming30-Jun-14 1:47 
Even without using reflection, you won't be able to cast a List<Person> to a List<object>, because that would let you break the list.

Consider what would happen if this cast was allowed:
C#
List<Person> onlyContainsPeople = new List<Person>();
List<object> containsAnyObject = (List<object>)onlyContainsPeople;
containsAnyObject.Add(new Hippopotamus()); // <-- Not a Person!
Person thePerson = onlyContainsnPeople[0];

You now have a list which is guaranteed to only contain Person objects, but the only object in the list is not a Person object. Either the line which adds the item to the list would have to throw an exception, or the line which retrieves the item from the list would have to throw an exception. Neither exception would be obvious or expected.


There are a few generic interfaces and delegates which, in .NET 4.0 or higher, are declared as covariant or contravariant, which will allow something similar to what you're trying to do:
C#
List<Person> onlyContainsPeople = new List<Person>();
IEnumerable<object> containsAnyObject = onlyContainsPeople;

Generic covariance only works for read-only interfaces/delegates - the generic type parameter is only ever returned, never passed in. Contravariance only works when the type parameter is only ever passed in, never returned.

See Covariance and Contravariance in Generics[^] on MSDN for more information.



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


QuestionThesis Project Pin
jomilyn28-Jun-14 3:02
jomilyn28-Jun-14 3:02 
AnswerRe: Thesis Project Pin
Pete O'Hanlon28-Jun-14 3:07
mvePete O'Hanlon28-Jun-14 3:07 
GeneralRe: Thesis Project Pin
jomilyn28-Jun-14 3:33
jomilyn28-Jun-14 3:33 
GeneralRe: Thesis Project Pin
OriginalGriff28-Jun-14 3:59
mveOriginalGriff28-Jun-14 3:59 
GeneralRe: Thesis Project Pin
jomilyn28-Jun-14 4:07
jomilyn28-Jun-14 4:07 
GeneralRe: Thesis Project Pin
OriginalGriff28-Jun-14 4:11
mveOriginalGriff28-Jun-14 4:11 
GeneralRe: Thesis Project Pin
Tom Delany30-Jun-14 12:06
Tom Delany30-Jun-14 12:06 
Questioncannot found emailhandler in c# 2008 Pin
KaKoten27-Jun-14 22:19
KaKoten27-Jun-14 22:19 
AnswerRe: cannot found emailhandler in c# 2008 Pin
OriginalGriff27-Jun-14 22:34
mveOriginalGriff27-Jun-14 22:34 
GeneralRe: cannot found emailhandler in c# 2008 Pin
KaKoten27-Jun-14 22:41
KaKoten27-Jun-14 22:41 
GeneralRe: cannot found emailhandler in c# 2008 Pin
OriginalGriff27-Jun-14 22:45
mveOriginalGriff27-Jun-14 22:45 
GeneralRe: cannot found emailhandler in c# 2008 Pin
KaKoten27-Jun-14 22:57
KaKoten27-Jun-14 22:57 
GeneralRe: cannot found emailhandler in c# 2008 Pin
OriginalGriff27-Jun-14 23:52
mveOriginalGriff27-Jun-14 23:52 
GeneralRe: cannot found emailhandler in c# 2008 Pin
KaKoten28-Jun-14 12:36
KaKoten28-Jun-14 12:36 
GeneralRe: cannot found emailhandler in c# 2008 Pin
OriginalGriff28-Jun-14 22:17
mveOriginalGriff28-Jun-14 22:17 
QuestionForm not allowed to move or choose another form in C# ? Pin
Member 245846727-Jun-14 19:01
Member 245846727-Jun-14 19:01 
AnswerRe: Form not allowed to move or choose another form in C# ? Pin
OriginalGriff27-Jun-14 20:34
mveOriginalGriff27-Jun-14 20:34 

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.