I have to create a list, but i only know the class name
public void getList(string className)
{
IList lsPersons = (IList)Activator.CreateInstance(
typeof(List<>).MakeGenericType(Type.GetType(className))));
}
I tried so many methods but nothing works for me.
You can make a generic list but it is not useful. If you want to have a generic List<T> , somewhere you SHOULD incorporate your prior knowledge about the demanded type. For example you can do something like this:
if(className == "Employee") // this is where your prior knowledge is playing role
{
IList<Employee> lsPersons = (IList<Employee>)Activator.CreateInstance(
typeof(List<Employee>).MakeGenericType(Type.GetType(className))));
}
Also, you can make a generic list of any type via something like this:
public static class GenericListBuilder
{
public static object Build(Type type)
{
var obj = typeof(GenericListBuilder)
.GetMethod("MakeGenList", BindingFlags.Static|BindingFlags.NonPublic)
.MakeGenericMethod(new Type[] { type })
.Invoke(null, (new object[] {}));
return obj;
}
private static List<T> MakeGenList<T>()
{
return new List<T>();
}
}
And it is possible to consume it like:
var List<Employee> = GenericListBuilder.Build(typeof(Employee)) as List<Employee>;
or
IList list = GenericListBuilder.Build(Type.GetType(className)) as IList;
The last line is completely blind and I think it is very close to what you have in mind. But does it have any benefit? I don't think.
Related
How would you assign a list to a generic list since they are not the same type.
If I have a generic list:
List<T> myList = new List<T>();
and I have another list
List<OtherType> otherList = new List<OtherType>();
After I fill otherList with values. What are ways I can assign otherList to the generic list? Preferably without using a foreach.
if they are the same type you can do a basic type conversion
if(typeof(T) == typeof(OtherType))
myList = otherList as List<T>;
But that would make no sense, so I'd imagine you need some kind of conversion, problem is we need to specify that T is assignable from your base class
public static class StaticFoo
{
public static List<T> Foo<T>() where T : class
{
List<MyOtherClass> returnList = new List<MyOtherClass>() { new MyOtherClass() };
if(typeof(T).IsAssignableFrom(typeof(MyOtherClass)))
return returnList.Select(x => x as T).ToList();
throw new Exception($"Cannot convert {typeof(T)} to MyOtherClass");
}
}
public class MyClass { }
public class MyOtherClass : MyClass { }
The above code will work if you call it with T = MyClass or any other class that myOtherClass can be cast to. Alternatively you might want a concrete conversion method for a set of predefined types, it's kind of hacky but you could do something like this
public static class StaticFoo
{
public static List<T> Foo<T>() where T : class
{
List<MyOtherClass> returnList = new List<MyOtherClass>() { new MyOtherClass() };
return returnList.Select(x => x.Convert(typeof(T)) as T).ToList();
}
}
public class MyOtherClass {
public object Convert(Type type) {
if (type == typeof(string)) //more if statements for more types
return this.ToString(); //just an example
throw new NotImplementedException($"No cast available for type {type}");
}
}
Some context for the relationship between the generic type and your concrete class would be helpful
edit:
some advice that ignores your actual question. Most likely, you want to create an interface and return a list of that interface (I'm assuming that will match your use case more closely). Alternatively just change the signature to return List< object> - then you can do
return otherList.ToList<object>();
List<T> is invariant, so you can only assign lists of the same type. The closest you can come is creating a new list with the same items.
List<T> list = otherList.Select( x => (T)x ).ToList();
How can I convert these two ConvertDtoListToAddresses and ConvertDtoListToDocuments C# methods to a generic? I've tried passing in two generic type variables, but when I get down to 'Add' in the loop I get stuck on various errors. Converting from a DTO to its respective DBO is done in the constructor of the DBO, which I think is part of the problem.
private void ConvertDtoToPerson(PersonDTO dto)
{
Id = dto.Id;
Fname = dto.FirstName;
Mname = dto.MiddleName;
Lname = dto.LastName;
Suffix = dto.Suffix;
Maiden = dto.MaidenName;
Addresses = ConvertDtoListToAddresses(dto.AddressesDto); // want to convert to generic
Documents = ConvertDtoListToDocuments(dto.DocumentsDto); // want to convert to generic
}
private static ICollection<Address>? ConvertDtoListToAddresses(ICollection<AddressDTO>? addressesDto)
{
if (addressesDto is not null && addressesDto.Any())
{
ICollection<Address> addys = new List<Address>();
foreach (AddressDTO dto in addressesDto)
{
// Converts from dto in constructor
addys.Add(new Address(dto));
}
return addys;
}
return null;
}
private static ICollection<Document>? ConvertDtoListToDocuments(ICollection<DocumentDTO>? documentsDto)
{
if (documentsDto is not null && documentsDto.Any())
{
ICollection<Document> docs = new List<Document>();
foreach (DocumentDTO dto in documentsDto)
{
// Converts from dto in constructor
docs.Add(new Document(dto));
}
return docs;
}
return null;
}
Here is what I tried:
Addresses = ConvertDtoListToType<Address, AddressDTO>(dto.AddressesDto);
private static ICollection<T>? ConvertDtoListToType<T, D>(ICollection<D>? dtoCollection)
{
if (dtoCollection is not null && dtoCollection.Any())
{
ICollection<T> tList = new List<T>();
foreach (D dto in dtoCollection)
{
tList.Add(new T(dto)); // <-- This is where I'm having trouble
}
return tList;
}
return null;
}
Use of a Func<D, T> factory parameter would sort this out.
private static ICollection<T>? ConvertDtoListToType<T, D>(ICollection<D>? dtoCollection, Func<D, T> factory)
{
if (dtoCollection is not null && dtoCollection.Any())
{
ICollection<T> tList = new List<T>();
foreach (D dto in dtoCollection)
{
tList.Add(factory(dto));
}
return tList;
}
return null;
}
Do keep in mind that that is almost the semantic equivalent of this:
private static ICollection<T>? ConvertDtoListToType<T, D>(ICollection<D>? dtoCollection, Func<D, T> factory)
=> dtoCollection?.Select(d => factory(d))?.ToList();
I'd question the idea that an empty dtoCollection should return a null final collection anyway. This is probably a better implementation.
So, having said that, your original method offers very little functionality benefit. It's code for code's sake. A simple Select/ToList pair keeps your code simple.
In any case, you can provide a static method off of Address and Document to provide the Func<D, T> that you need.
public class Address
{
AddressDTO dto;
public static Address CreateFromDto(AddressDTO dto)
=> new Address(dto);
public Address(AddressDTO dto)
{
this.dto = dto;
}
}
Now, calling it is like this:
var addresses = ConvertDtoListToType(addressDtos, Address.CreateFromDto);
Or:
var addresses = addressDtos?.Select(Address.CreateFromDto)?.ToList();
What you need is to be able to provide a constraint on the Type T to say that it has a constructor that takes a parameter of type D. Something like this:
private static ICollection<T>? ConvertDtoListToType<T, D>(
ICollection<D>? dtoCollection) where T : new(D)
{}
But this does not exist in C#. The workaround is to provide a factory method to create your type T given a type D. i.e. Something like:
private static ICollection<T>? ConvertDtoListToType<T, D>(
ICollection<D>? dtoCollection, Func<D, T> factory)
{
// Use factory(dto), instead of new T(dto)
}
But as #Zee says, you should have a look at Automapper, which can convert between types of collections.
EDIT: I just realized that what I want to do follows the Dependency Injection pattern that I learned from Mark Seemann who wrote an excellent book on the subject. I want to have a method where I do some setup, like adding rows, but pass in an object that writes the rows to the context. The BulkInsert method is for large "setups" and the AddRange is for smaller setups.
I have a static method where I'm passing in my DbContext, and I'm updating rows to several entities. Depending on how I call the method, I want to either add the rows using BulkInsert or AddRange
public static void MySetup(MyContext, int addMethod)
{
var list1 = new List<Foo>{......}
if (addMethod = 1)
context.BulkInsert(list1);
else
context.AddRange(list1);
var list2 = new List<Bar>{......}
if (addMethod = 1)
context.BulkInsert(list2);
else
context.AddRange(list2);
}
Instead of passing in a variable, I'd like to learn how I can pass in a function instead, so it would like something like this:
public static void MySetup(MyContext context, ??? myAdd)
{
var list1 = new List<Foo>{......}
myAdd(context, list1);
var list2 = new List<Bar>{......}
myAdd(context, list2);
}
I think what I'm looking for is a delegate. Note that I use different types of List classes, so whatever functions I setup on the calling end would need to accept a generic list (is that ?) because I wouldnt want to have to pass in a function for every possible type.
ETA:
BulkInsert and AddRange are both adding rows via EntityFramework. And they each take a List. However BulkInsert has an optional options parameter so it may be called like context.BulkInsert(myList, opts)
I think all you need is a delegate who accepts an IList but it is difficult to know without knowing the signature of the AddRange and AddBulk methods.
public static void MySetup(MyContext context, Action<MyContext, IList> myAdd)
{
var list1 = new List<Foo> { };
myAdd(context, list1);
var list2 = new List<Bar> { };
myAdd(context, list2);
}
...
Action<MyContext, IList> insertRange = (context, list) =>
{
context.AddRange(list);
};
Action<MyContext, IList> insertBulk = (context, list) =>
{
context.BulkInsert(list);
};
MySetup(new MyContext(), insertRange);
// or...
MySetup(new MyContext(), insertBulk);
I thought long about if I should post it (as it does kind of answer the question but I don't think it solves the problem behind) but so be it:
If you know all the possible types you are going to use up front the easiest way would be this:
interface IAddStuff
{
void Add (MyContext context, IList<Foo> list);
void Add (MyContext context, IList<Bar> list);
}
class Operations
{
public static void MySetUp(MyContext context, IAddStuff adder)
{
var list1 = new List<Foo> ();
adder.Add (context, list1);
var list2 = new List<Bar> ();
adder.Add (context, list2);
}
}
class SampleImplementation : IAddStuff
{
public void Add(MyContext context, IList<Foo> list)
{
// ...
}
public void Add(MyContext context, IList<Bar> list)
{
// ...
}
}
Now if you don't know exactly you can still do runtime-dispatch (and let the implementation decide):
class MyContext {}
class Foo {}
class Bar {}
interface IAddStuff
{
void Add<T> (MyContext context, IList<T> list);
}
class Operations
{
public static void MySetUp(MyContext context, IAddStuff adder)
{
var list1 = new List<Foo> ();
adder.Add (context, list1);
var list2 = new List<Bar> ();
adder.Add (context, list2);
}
}
class SampleImplementation : IAddStuff
{
public void Add<T> (MyContext context, IList<T> list)
{
if (typeof(T) == typeof(Foo))
AddFoo(context, list.Cast<Foo>());
else if (typeof(T) == typeof(Bar))
AddBar(context, list.Cast<Bar>());
}
void AddFoo(MyContext context, IEnumerable<Foo> list)
{
// ...
}
void AddBar(MyContext context, IEnumerable<Bar> list)
{
// ...
}
}
Now as you can see you only moved the problem (the runtime check for what kind of generic type you really have) into the implementation of said interface but you can keep your other code a bit more clean.
Don't know if you find this useful but this is the nearest I can come to what I think you asked.
PS it should be obvious of how you can update this with some parameter to tell if it should use bulk-insert or not.
PPS and yeah you can misuse the dynamic stuff to get the dynamic-dispatch without the ifs ... but I don't like those very much - if you are interested in it anyway here is an example where this trick is used
If Foo and Bar share a common interface, then you can pass the method in using the Action delegate.
For example:
public interface IBaz
{
}
public class Foo : IBaz
{
}
public class Bar : IBaz
{
}
public static void MySetup(DurianContext context, Action<DurianContext, List<IBaz>> addMethod)
{
List<IBaz> list1 = new List<IBaz>();
addMethod(context, list1);
List<IBaz> list2 = new List<IBaz>();
addMethod(context, list2);
}
I 'm trying to build a DI container and I 've stumbled on to the following problem: I have a method that retrieves a list of registered instances for a given type and I want to use that to inject IEnumerable<T> properties in a given object. An example of what I am trying to achieve would be the following:
class A { public IList<IExample> Objects { get; set; } }
class B: IExample {}
class C: IExample {}
Container.Register<IExample>(new B());
Container.Register<IExample>(new C());
var obj = new A();
Container.Inject(A);
Debug.Assert(A.Objects != null && A.Objects.Count == 2);
My Retrieve method returns an IList<object>, mainly because I have no type information at that moment, so I am attempting to convert that list into a List<T> at injection time. Here is a succint form of the methods doing the work:
public virtual IList<object> Retrieve(Type type)
{
var instances = Registry[type];
foreach(var instance in instances)
Inject(type, instance); // omitted
return instances;
}
public virtual void Inject<T>(T instance)
{
var properties = typeof (T).GetProperties();
foreach (var propertyInfo in properties)
{
var propertyType = propertyInfo.PropertyType;
if (!IsIEnumerable(propertyType)) continue;
var genericType = propertyType.GetGenericArguments()[0];
propertyInfo.SetValue(instance,
GetListType(genericType, Retrieve(genericType)), null);
}
}
protected virtual object GetListType(Type type, IEnumerable<object> items)
{
return items.Select(item => Convert.ChangeType(item, type)).ToList();
}
The code returns the error: System.InvalidCastException : Object must implement IConvertible. Sadly, I don't know how to proceed from here. Perhaps I am doing this all wrong. I 've thought of using generics or injecting multiple properties by hand, but I'd really like to not have to do that.
Thanks in advance for any help or ideas.
You could create a generic list like this:
public virtual IList Retrieve(Type type)
{
// ...
listType = typeof(List<>).MakeGenericType(new Type[] { type });
IList list = (IList)Activator.CreateInstance(listType);
// ...
return list
}
this list can be casted to IList<T>, because it is one.
You could consider to use IEnumerable and Cast<T>, but then you don't have an instance of a list. I don'^t know how important it is to have one.
i'm trying to make a mixed collection of Types. I know the types at the start.. but I can't seem to figure out the syntax to make the collection, etc.
eg.
....
// I leave the typo there, for embarrassment :(
Initialize(new []{ typeof(Cat), typeof(Dog), typeof(JohnSkeet) });
...
public Foo Initialize(IEnumerable<Type> types)
{
// for each type, set up the inmemory storage.
foreach(var type in types)
{
// ????
// Create an empty list, which will only contain this 'type'
// I'm guessing, an IDictionary<type, ICollection<type>>().. thingy ?
}
}
public ICollection<Type> SomeTypeData(Type type)
{
// Return the collection, for this type.
}
Does this mane sense? Is this possible?
Okay, now that I think I know what you want, it would look something like this:
// This can't really be *properly* statically typed
private readonly Dictionary<Type, object> typeMap = new
Dictionary<Type, object>();
public Foo Initialize(IEnumerable<Type> types)
{
Type genericListType = typeof(List<>);
foreach(var type in types)
{
// MakeGenericType is really badly named
Type constructedListType = genericListType.MakeGenericType(type);
typeMap[type] = Activator.CreateInstance(constructedListType);
}
}
// We can't express this particularly safely either,
// although we *could* return the non-generic IList
public object SomeTypeData(Type type)
{
return typeMap[type];
}
// This *is* statically typed, although we need to cast inside
public IList<T> SomeTypeData<T>()
{
return (IList<T>) typeMap[typeof(T)];
}
See this blog post for a similar example.
Note that basically you're trying to represent something which generics simply can't handle, in terms of the internal dictionary type... and the first form of SomeTypeData can't be statically typed either... because that means knowing the type at compile time when we'll only actually be given it at execution time.
It looks to me like you're trying to create some kind of instance repository; a class that stores a list of instances of a given type.
Here's an example implementation. I've included both a generic and non-generic version of the SomeTypeData method:
public class InstanceRepository
{
private IDictionary<Type, ICollection> _Instances = new Dictionary<Type, ICollection>();
public ICollection SomeTypeData(Type type)
{
ICollection instanceList;
if (!_Instances.TryGetValue(type, out instanceList))
{
// this type does not exist in our dictionary, so let's create a new empty list
// we could do this:
//instanceList = new List<object>();
// but let's use reflection to make a more type-specific List<T> instance:
instanceList = (ICollection)Activator.CreateInstance(typeof(List<>).MakeGenericType(type));
// now add it to the dictionary
_Instances.Add(type, instanceList);
}
// Return the collection, for this type.
return instanceList;
}
public IList<T> SomeTypeData<T>()
{
Type type = typeof(T);
ICollection instanceList;
if (!_Instances.TryGetValue(typeof(T), out instanceList))
{
instanceList = new List<T>();
_Instances.Add(type, instanceList);
}
// here we are assuming that all of the lists in our dictionary implement IList<T>.
// This is a pretty safe assumption, since the dictionary is private and we know that
// this class always creates List<T> objects to put into the dictionary.
return (IList<T>)instanceList;
}
}
Below is a usage example:
Generic:
InstanceRepository repository = new InstanceRepository();
var listOfCats = repository.SomeTypeData<Cat>();
listOfCats.Add(new Cat());
Cat firstCat = listOfCats[0];
Console.WriteLine(listOfCats.GetType().FullName);
Non-Generic:
InstanceRepository repository = new InstanceRepository();
var listOfCats = (IList<Cat>)repository.SomeTypeData(typeof(Cat));
listOfCats.Add(new Cat());
Cat firstCat = listOfCats[0];
Console.WriteLine(listOfCats.GetType().FullName);
I guess you want something like
_dict[typeof(Cat)]=new List<Cat>();
_dict[typeof(Dog)]=new List<Dog>();
only programatically based on given types?
Something like this should work:
public void Initialize(IEnumerable<Type> types)
{
foreach(var type in types)
{
var list = Activator.CreateInstance(Type.GetType("System.Collections.Generic.List`1").MakeGenericType(type));
_cache[type] = list;
}
}
public ICollection<T> Get<T>()
{
object list;
if (_cache.TryGetValue(typeof(T), out list)
{
return list as ICollection<T>;
}
else
{
...
}
}
var cats = Get<Cat>();
I'm not sure I fully understand you're question, but if you already have an IEnumerable<Type> which contains an enumeration of Type objects, then why not just use that to initialize some type of Collection (such as List<Type>)?
public ICollection<Type> Initialize(IEnumerable<Type> types)
{
ICollection<Type> collection = new List<Type>(types);
return collection;
}