c# - Access property/variable from unknown object -


i have list of 'object' has different kinds of objects in it. need able edit/read property of objects of them have, don't know how that.

list<object> objects = new list<object>(); someobject obj;  objects.add(obj);  int value = objects(0).somevariable; 

but can't read 'somevariable' since code doesn't know object has it. doing possible since objects in list has variable 'somevariable'?

also, i'm using xna, doesn't make difference.

edit: answers, i'll try them tomorrow when on computer.

if can modify classes , make sense on scenario, define common interface

public interface imyinterface {      int somevariable { get; set;} } 

and let classes implement interface, able create generic list this

var objects = new list<imyinterface>(); int value = objects[0].somevariable; 

Comments

Popular posts from this blog

user interface - Python attempting to create a simple gui, getting "AttributeError: 'MainMenu' object has no attribute 'intro_screen'" -

jquery - Common JavaScript snippet to share files on Google Drive, Dropbox, Box.net or SkyDrive -

Android Gson.fromJson error -