pass class type as parameter c# and used with dictionary
Solution 1
Using Generics
Your current approach will give you much trouble. As you are going to pass a Type object for your class, you will need reflection to be able to create the Dictionary
.
As an alternative, I propose to you to create a generic method:
public Dictionary<object, object> GetDetails<TClass>()
{
MvcDemoDBEntities db = new MvcDemoDBEntities();
Dictionary<TClass, object> dict = new Dictionary<TClass, object>();
var data = (from h in db.People
select h).ToList();
foreach (var item in data)
{
dict.Add(data, true);
}
return dict;
}
Use it like this:
List<people> list = GetDetails<people>().Keys.ToList();
Using Type
Of course, this can be done using a Type object, this requiers the use of reflection to be able to create an object of which type we don't know (that object is the dictionary). This is done as follows:
public Dictionary<object, object> GetDetails(Type Class)
{
//Null check
if (null == Class)
{
throw new ArgumentNullException("Class");
}
MvcDemoDBEntities db = new MvcDemoDBEntities();
//Get the generic dictionary type:
Type DictType = typeof(Dictionary<,>).MakeGenericType(Class, typeof(object));
//Create the dictionary object:
object dict = Activator.CreateInstance(typeof(DictType));
//Get the Add method:
var add = DictType.GetMethod("Add", new Type[]{Class, typeof(object)});
var data = (from h in db.People
select h).ToList();
foreach (var item in data)
{
//add to the dictionary:
add.Invoke(dict, new object[]{item, true});
}
return dict;
}
Use like this:
List<people> list = GetDetails(typeof(people)).Keys.ToList();
Digging deeper
I notice you have this line:
var data = (from h in db.People select h).ToList();
You may be interested in changing People
to a property matching the name of the class you pass in. This can only be archived via reflection. In a similar way as how we got the Add
method of the dictionary, we can get a property from the object db
which name is given by the argument type.
I'll present this as a second method to be called by the first.
Using Generics
public IEnumerable<TClass> GetCollection<TClass>(MvcDemoDBEntities db)
{
//get the type of db
var dbType = db.GetType();
//get the name of the type TClass
var name = typeof(TClass).Name;
//get the property
var prop = dbType.GetProperty(name);
//read the property and return
return prop.GetValue(db);
}
To use, replace this:
var data = (from h in db.People select h).ToList();
With this:
var data = (from h in GetCollection<TClass>(db) select h).ToList();
Using Type
Here the struggle is that we don't know the item type... so I'll use IEnumerable.
public IEnumerable GetCollection(MvcDemoDBEntities db, Type Class)
{
//get the type of db
var dbType = db.GetType();
//get the name of the type Class
var name = Class.Name;
//get the property
var prop = dbType.GetProperty(name);
//read the property and return
return prop.GetValue(db);
}
To use, replace this:
var data = (from h in db.People select h).ToList();
With this:
var data = (from h in GetCollection(db, Class).Cast<object>() select h).ToList();
Solution 2
You need to use typeof
to pass the type of your class
List<people> list = GetDetails(typeof(people)).Keys.ToList();
Nayeem Mansoori
Nayeem Mansoori is a Microsoft geek and a keen developer interested in Microsoft Technologies and with professional experience of more than 6 years in .Net Technologies.
Updated on November 23, 2020Comments
-
Nayeem Mansoori over 3 years
I am create a function to return a dictionary and I want to pass class name through as a parameter. But its gives an error. Code i have written is given below
public Dictionary<object, object> GetDetails(Type Classname) { MvcDemoDBEntities db = new MvcDemoDBEntities(); Dictionary<Classname, object> dict = new Dictionary<Classname, object>(); var data = (from h in db.People select h).ToList(); foreach (var item in data) { dict.Add(data, true); } return dict; }
what am i doing wrong and i would like to call this function with a class name dynamically, like this :
List<people> list = GetDetails(people).Keys.ToList();
people its my class name.