Universal Windows Platform (UWP)
A Microsoft platform for building and publishing apps for Windows desktop devices.
2,934 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hello,
I am trying such code. I want to create a new object and add it to the list, but the class of the object is passed to the method.
public List<T> GetDataList<T>(T dataObject)
{
var dataList = new List<T>();
var newDataObject = new T();
dataList .Add(newDataObject );
return dataList;
}
Line: var newDataObject = new T(); doesnt work.
Is it possible to do my task, and how?
Hi, Your code must look like this:
public List<T> GetDataList<T>(T dataObject)
{
var dataList = new List<T>();
var newDataObject = (T)Activator.CreateInstance(typeof(T), dataObject);
dataList.Add(newDataObject);
return dataList;
}