SoFunction
Updated on 2025-03-07

c# Example of using reflection call type member


classProgram
{
staticvoidMain(string[]args)
{
Typetype=typeof(Employee);

// Use the parameterless constructor to create objects dynamically
varobjNull=(null,,null,null,null);

//Calling two constructors that use two string parameters to create objects dynamically
varfrankJob=(null,,null,null,newobject[]{"job","frank"});

//Calling the public member attribute get method
varfileName=("FirstName",,null,frankJob,null);

//Calling the public member attribute set method
("Email",,null,frankJob,newobject[]{"gyzdfasddfsafhao@"});

//Dynamic call to the method without parameters
varobjStr=("ToString",|||,null,frankJob,null);

//Dynamic call method with parameters
varemail=("GetEmail",,null,frankJob,newobject[]{"sunshine"});
}

publicclassEmployee
{
publicintId{get;set;}
publicstringFirstName{get;set;}
publicstringLastName{get;set;}
publicstringAddress{get;set;}
publicstringEmail{get;set;}

publicEmployee(){}
publicEmployee(stringfirstName,stringlastName)
{
FirstName=firstName;
LastName=lastName;
}

publicoverridestringToString()
{
("{0}{1}",LastName,FirstName);
}

publicstringGetEmail(stringuser)
{
("{0}@",user);
}
}
}