E
E
Eugene2013-01-14 10:36:59
Programming
Eugene, 2013-01-14 10:36:59

Connecting a DLL in C#: Run through IList

Hello.
Trying to implement modularity in the program, I ran into a small problem.
The function that I call is in the dll and returns the IList<User> type, and the program that connects the dll using Assembly does not know anything about the type, it only knows that this is a list and it has 3 elements.
How to run through them?



In other words, how to get the ID and login of each element of the list, what are the options to do this?
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
R
Rifat, 2013-01-14
@Qwofer

With Reflection

foreach (object value in returned)
{
  var id = value.GetType().GetProperty("Id").GetValue(value, null);
  var login = value.GetType().GetProperty("Login").GetValue(value, null);
}

A
Antelle, 2013-01-14
@Antelle

foreach (dynamic item in (dynamic)returned) {
    string login = item.Login;
}

P
Paulskit, 2013-01-14
@Paulskit

It can be made easier and more secure.

class User {
  public GUID Id {get;set;}
  public string Login {get;set;}
}

interface ITestInterface
{
  IList<User> GetList();
}

And get it like this:
 Activator.CreateInstance(type) as ITestInterface;

Get a strongly typed object as output. And no reflection is needed.

A
Alexander Ananiev, 2013-01-14
@SaNNy32

Maybe so?

foreach(var el in returned)
{
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question