I have a problem with which I can't get any further. Google brings me a lot of results, but I can't really get by.
I've tried different things with generic classes and reflections. Nothing worked. I have again minimized the following example so that only the most important things remain. I would be grateful for help.
roland
public class TestData1
{
public long Id { get; set; } = 0;
public string Firstname { get; set; }
public string Name { get; set; }
public string Fullname { get; set; }
public string Beruf { get; set; }
public DateTime Date { get; set; } = DateTime.Now;
}
public class TestData2
{
public long Id { get; set; } = 0;
public string Firstname { get; set; }
public string Name { get; set; }
public string Fullname { get; set; }
}
public class Doit
{
public Doit( ) {
// init two different classes with data
TestData1 td1 = new TestData1 {Firstname="Roland", Name="Smith", Beruf="Developer"};
TestData2 td2 = new TestData2 {Firstname="Petra", Name="Summer" };
// call the function to do the same action with the diferent classes
TestData1 tf1 = TestFunc<TestData1>(td1, "OtherParams" );
TestData2 tf2 = TestFunc(td2, "OtherParams");
}
/// <summary>
/// Example
/// All entries dosn't work. :(
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="prog"></param>
/// <param name="textInserts"></param>
/// <returns></returns>
private T TestFunc<T>( T prog, string textInserts = null ) where T : class {
string prgname = prog.GetType().Name; // typeof(prog).Name;
// Get two propertiers from the class. Ths properties aare in
// all classes the same.
var aa = T.Firstname;
var bb = T.Name;
// do somethin with the class and the properties
T.Fullname = $"{aa} {bb}";
// Do some others with the class and the properties.
// return the class with the changed data
return T;
}
}
Aucun commentaire:
Enregistrer un commentaire