I have a model like below
public sealed class Person
{
public string MobileNo { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
}
In my implmentation class, I have a method which takes an IEnumerable
as a parameter
public string PersonList(string listName, IEnumerable<Person> persons)
{
dictionary.Add("name", new String[1] { listname });
dictionary.Add("list", persons.ToArray());
PrivateMethod("personList", dictionary);
}
I have another private method
private string PrivateMethod(string value, Dictionary<string, object[]> parameters)
{
foreach (KeyValuePair<string, object[]> kvp in parameters)
{
Person[] persons = kvp.Value.Cast<Person>().ToArray();
[...]
}
[...]
}
I want to make this above method reusable, and don't want to put "Person" model tightly-coupled.
Can I use dynamic ?
ContactList(string listName, IEnumerable<dynamic> persons)
And within the private method
dynamic[] persons = kvp.Value.Cast<
How to pass model here>().ToArray();
Solution:
This will work, great.
dynamic[] persons = kvp.Value.Cast<dynamic>().ToArray();
Thanks to usr & Rune FS
You could make the
PrivateMethod
generic:Have you tried using "Interfaces" in c#? you can cast any object to any type as long as they are all derived to the same type of interface.