Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2007

Tip: Looking for answers? Try searching our database.

General method to add any entity to its entity list

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Andrus - 23 Sep 2007 22:50 GMT
I'm creating ComboBox class which uses entity list as datasource where
entity class name is passed as string. Entity list is filled dynamically at
run time.

So need to add entities of different types to corresponding entity lists.
I created general Add() method for this.

This method call causes exception from my code
"List does not have Add method"

How to fix ?

Andrus.

using System.Collections.Generic;
using System;
using System.Reflection;

class TestApplication {

static void Main() {
object customerList = Customer.GetEmptyList();
Add(customerList, new Customer());
}

// General routine to add entity to its corresponding list.
static void Add(object entityList, object entity) {

Type type = entityList.GetType();
Type[] types = new Type[1];
types[0] = entity.GetType();

MethodInfo methodInfo = type.GetMethod("Add",
                     BindingFlags.Public, null, types, null);
if (methodInfo == null)
 // how to fix code so that this exception does not occur ?
 throw new ApplicationException("List does not have Add method");

object[] parameters = new object[1];
parameters[0] = entity;
methodInfo.Invoke(entityList, parameters);
}
}

class Customer : ActiveRecordBase<Customer> {}

public class ActiveRecordBase<T> {

public static List<T> GetEmptyList() {
return new List<T>();
}
}
Nicholas Paldino [.NET/C# MVP] - 24 Sep 2007 17:17 GMT
Andrus,

   From what it seems, you need to add the Instance binding flag to the
Public binding flag to get the method.

   I would recommend using an interface instead which will have the add
method on it, and then you can pass implementations of that list to your
method to add a new instance.  It would be much better than using
reflection.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> I'm creating ComboBox class which uses entity list as datasource where
> entity class name is passed as string. Entity list is filled dynamically
[quoted text clipped - 49 lines]
> }
> }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.