c# - Dynamic cast at runtime -
is there way dynamically cast @ runtime following pseudo code:
foreach (datarow row in table.rows) { foreach (datacolumn col in table.columns) { if (row[col] != dbnull.value) { type type = col.datatype; type cellcontent = (type)row[col]; //pseudo-code } } }
i´ve been searching web , not found anything. there´s object obj = activator.createinstance(type);
, i´m still stuck object , can´t use specific type methods it. need cast of existing object , not new instance. need remove eventhandlers cellcontent in cases cause memory leak, example: object type ilist[serialnumbergridviewmodel] , serialnumbergridviewmodel implements propertychanged-handler causing memory leak. idea? there way solve issue?
i´ve solved in above specific case, general method lot better, program i´m working big , has lot of memory leaks removed.
no. can't cast type unknown @ compile time. however, c# have special keyword declaring variables of type unknown - it's dynamic
.
can think of form of late binding - actual type of variable determind @ run time only.
when declare dynamic variable, c# compiler creates variable of type object, not perform type checking.
the dynamic type enables operations in occurs bypass compile-time type checking. instead, these operations resolved @ run time.
...
type dynamic behaves type object in circumstances. however, operations contain expressions of type dynamic not resolved or type checked compiler. compiler packages information operation, , information later used evaluate operation @ run time. part of process, variables of type dynamic compiled variables of type object. therefore, type dynamic exists @ compile time, not @ run time.
Comments
Post a Comment