1

i have a function like this, it get a typeof T, cipher the properties(all properties are string):

private T Cipher(T item)
    {
        var t = item.GetType();
        //get all properties of item and put in a list 
        var props=new List<PropertyInfo>(t.GetProperties());
        var vals= props.Select(propertyInfo =>
                               propertyInfo.GetValue(item).ToString()).ToList();
        var cipher = new List<string>();
        foreach (var val in vals)
        {
            cipher.Add(CipherString.Encrypt(val,"key");
        }

is there any way to create a new typeof T property with new values of cipher? Edit: T is class definition with some properties

Arsalan
  • 709
  • 2
  • 14
  • 27
  • Setting property by reflection http://stackoverflow.com/questions/1089123/setting-a-property-by-reflection-with-a-string-value Create instance : https://msdn.microsoft.com/en-us/library/wccyzw83%28v=vs.110%29.aspx – puko Feb 17 '15 at 10:02

2 Answers2

0

After getting properties but instead of getting values you can do this

var ciphered = Activator.CreateInstance<T>();
foreach (var property in props)
    property.SetValue(ciphered, CipherString.Encrypt(property.GetValue(item, null) as string, "key"));
return ciphered;
Demarsch
  • 1,419
  • 19
  • 39
  • in var ciphered = Activator.CreateInstance(); System.MissingMethodException occures! – Arsalan Feb 17 '15 at 10:14
  • This method was introduced in .NET framework 3.5 (if I'm correct). If you are using version prior to this you can use code `var ciphered = Activator.CreateInstance(typeof(T)) as T;` – Demarsch Feb 17 '15 at 10:19
  • I'm using .NET framework 4.5 and that exception occured again!! and thnks – Arsalan Feb 17 '15 at 10:30
  • @AliTheOne: Do you have a parameterless constructor in your T-class? – Patrick Feb 17 '15 at 13:13
  • No, i found the answer:because my class type doesn't define in .Net so i should use this :'FormatterServices.GetUninitializedObject(T.GetType());' instead of Activator.CreateInstance(typeof(T)) thanks – Arsalan Feb 17 '15 at 13:51
-1

Try use conversion like (T)(Object). Example.

 private T SomeFunction(T arg)
 {
        var cipher = new List<string>();
        cipher.Add("Test");
        cipher.Add("Test 2");
        cipher.Add("Test 3");
        return (T)(Object)cipher;
 }

Sample

class Test<T> where T: IEnumerable<string>, ICollection<string>
{
    public T Copy(T src)
    {
        List<string> result = new List<string>();
        foreach (var s in src as ICollection<string>)
            result.Add(s);
        return (T)(Object)result;
    }

    public void Print(T what)
    {
        foreach (var s in what as ICollection<string>)
            Console.WriteLine(s);
    }
}

////////////////////////////////////////   

Test<List<string>> t = new Test<List<string>>();
t.Print(t.Copy(new List<string>() { "One", "Two" }));
foolsoft
  • 147
  • 6
  • 1
    There is no conversion between List and generic T types so you'll get InvalidCastException – Demarsch Feb 17 '15 at 10:05
  • for some reason you suggest that the class T is a collection of strings. But the asker want to get all the properties of T object and not its contents – Demarsch Feb 17 '15 at 12:44