c# - Create instance of an unknown type and work with it -


I have a controller in MVC that checks the role of authenticated user. The basis of its role on the role of making an example of a class For example, if the role is "student", then I have to make an example of a student class. I am trying to do something like that but it does not work:

  Private user current user; Switch (roll) {case "student": current user = context.students. FirstOther Default (STD = & gt; STD.USIRID == WebSure.ContentUserIDID); break; . . . }  

But I can not define student properties, for example, when I want to get CurrentUser.firstname, no such property is available! How can I retrieve the current user?

You defined CurrentUser as the object type. This will not have the properties of student , unless you insert it.

  Private User Current User;  

You can use an interface or common base class, e.g.

  public class person {public string first name {get; Set; }} Public class students: Add special properties for persons {// students} Professors of the public: person {// Add special properties for the professors}  

then

 Use  Private person current user; All properties available for   

person will be available at CurrentUser . If you need access to such property which is only on student , then you can cast such a way:

  (Student current user). Some decent theme  

Comments

Popular posts from this blog

winforms - C# Form - Property Change -

java - Messages from .properties file do not display UTF-8 characters -

javascript - amcharts makechart not working -