c# - Generic Return Type -
i write class provides me decrypted values given domain model. following logic works fine, don't know declare in place of xxxxxx
.
sample domain model:
public class emp { public string empname{get;set;} }
i call decryptbyobject
method like:
var x = decryptbyobject(emp,key);
decryptbyobject
method:
public xxxxx decryptbyobject(xxxxxx myobject, string decryptkey) { type t = myobject.gettype(); foreach (memberinfo mi in t.getmembers()) { try { if (mi.membertype == membertypes.property) { string value = ((propertyinfo)mi).getvalue(myobject).tostring(); var bytes = convert.frombase64string(value); var decryvalue = machinekey.unprotect(bytes, decryptkey); ((propertyinfo)mi).setvalue(myobject, encoding.utf8.getstring(decryvalue)); } } catch (exception ex) { } } return myobject; }
you should use below.
public t decryptbyobject<t>(t myobject, string decryptkey) { type t = myobject.gettype(); propertyinfo prop = t.getproperty("items"); object list = prop.getvalue(myobject); foreach (memberinfo mi in t.getmembers()) { try { if (mi.membertype == membertypes.property) { string value = ((propertyinfo)mi).getvalue(myobject).tostring(); var bytes = convert.frombase64string(value); var decryvalue = machinekey.unprotect(bytes, decryptkey); ((fieldinfo)mi).setvalue(myobject, encoding.utf8.getstring(decryvalue)); } } catch (exception ex) { } } return myobject; }
Comments
Post a Comment