문제

string attribute = doc.SelectSingleNode("/class/@name").Value.ToString();
ObjectHandle employee = Activator.CreateInstance("EmployeeData", attribute);
Object employeeObject = employee.Unwrap();
return employeeObject;

'EmployeeData' is the current executing assembly. Attribute is selected from entry node of an XML file. I need to create and return an object in a more type safe way, without using ObjectHandle and UnWrap(). Can anyone help?

도움이 되었습니까?

해결책

It sounds like this would do the job:

var type = Assembly.GetExecutingAssembly().GetType(attribute);
return Activator.CreateInstance(type);

Using the CreateInstance(Type) overload guarantees that the assembly defining the type is already loaded, so that overload is free to return a straight object.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top