Click here to Skip to main content
15,892,746 members
Home / Discussions / C#
   

C#

 
GeneralRe: a better way to make, and populate, a List<Type> ? Pin
PIEBALDconsult7-Aug-12 16:54
mvePIEBALDconsult7-Aug-12 16:54 
GeneralRe: a better way to make, and populate, a List ? Pin
BillWoodruff7-Aug-12 18:06
professionalBillWoodruff7-Aug-12 18:06 
GeneralRe: a better way to make, and populate, a List ? Pin
BillWoodruff9-Aug-12 8:35
professionalBillWoodruff9-Aug-12 8:35 
GeneralRe: a better way to make, and populate, a List ? Pin
PIEBALDconsult9-Aug-12 12:16
mvePIEBALDconsult9-Aug-12 12:16 
GeneralRe: a better way to make, and populate, a List ? Pin
SledgeHammer017-Aug-12 18:58
SledgeHammer017-Aug-12 18:58 
GeneralRe: a better way to make, and populate, a List ? Pin
PIEBALDconsult8-Aug-12 3:32
mvePIEBALDconsult8-Aug-12 3:32 
GeneralRe: a better way to make, and populate, a List ? Pin
DaveyM698-Aug-12 23:10
professionalDaveyM698-Aug-12 23:10 
GeneralRe: a better way to make, and populate, a List ? Pin
Pete O'Hanlon9-Aug-12 0:51
mvePete O'Hanlon9-Aug-12 0:51 
Bill, the code you have there looks to be about as optimised as you are going to get. The only thing I would wonder about, though, is the extensibility of your model here. Basically, what you have here could end up violating the Open-Closed principal which states that a class should be open to extension but closed to modification.

So, what does that fancy term mean? Well, it simply means that if you decide to add another type in future, you have to crack open your code and modify it. It's up to you to decide whether or not this is acceptable to you. There are various ways to solve this problem, but one way could simply be to store the types in your configuration (comma delimited would do), and then parse them on the way in. So, suppose you store them in a key in app.config (for example), what would the code look like? Well, this should help:
C#
private void PopulateOkToModifyCollection()
{
  string types = ConfigurationManager.AppSettings["ModifiableTypes"];
  if (string.IsNullOrWhitespace(types))
    return;
  string[] allowedTypes = types.Split(',');
  foreach (string allowed in allowedTypes)
  {
    Type type = Type.GetType(allowed);
    if (type == null)
    {
      throw new ArgumentException(string.Format("The type {0} is not a valid type", allowed));
    }
    OkToModify.Add(type);
  }
}
Note that when you add the type to your comma separated list in the AppSettings key, it should be the fully qualified type.

*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

"Mind bleach! Send me mind bleach!" - Nagy Vilmos


CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

QuestionGet Variable Type from String Pin
eddieangel7-Aug-12 11:16
eddieangel7-Aug-12 11:16 
GeneralRe: Get Variable Type from String Pin
PIEBALDconsult7-Aug-12 13:18
mvePIEBALDconsult7-Aug-12 13:18 
GeneralRe: Get Variable Type from String Pin
BillWoodruff7-Aug-12 16:38
professionalBillWoodruff7-Aug-12 16:38 
GeneralRe: Get Variable Type from String Pin
SledgeHammer017-Aug-12 18:55
SledgeHammer017-Aug-12 18:55 
GeneralRe: Get Variable Type from String PinPopular
SledgeHammer017-Aug-12 18:52
SledgeHammer017-Aug-12 18:52 
GeneralRe: Get Variable Type from String Pin
PIEBALDconsult8-Aug-12 3:46
mvePIEBALDconsult8-Aug-12 3:46 
AnswerRe: Get Variable Type from String Pin
Shameel8-Aug-12 4:49
professionalShameel8-Aug-12 4:49 
GeneralRe: Get Variable Type from String Pin
SledgeHammer018-Aug-12 7:39
SledgeHammer018-Aug-12 7:39 
GeneralRe: Get Variable Type from String Pin
eddieangel8-Aug-12 6:01
eddieangel8-Aug-12 6:01 
GeneralRe: Get Variable Type from String Pin
eddieangel8-Aug-12 6:23
eddieangel8-Aug-12 6:23 
GeneralRe: Get Variable Type from String Pin
SledgeHammer018-Aug-12 7:44
SledgeHammer018-Aug-12 7:44 
GeneralRe: Get Variable Type from String Pin
eddieangel8-Aug-12 8:54
eddieangel8-Aug-12 8:54 
GeneralRe: Get Variable Type from String Pin
Pete O'Hanlon8-Aug-12 9:27
mvePete O'Hanlon8-Aug-12 9:27 
GeneralRe: Get Variable Type from String Pin
SledgeHammer018-Aug-12 9:32
SledgeHammer018-Aug-12 9:32 
GeneralRe: Get Variable Type from String Pin
SledgeHammer018-Aug-12 9:33
SledgeHammer018-Aug-12 9:33 
GeneralRe: Get Variable Type from String Pin
eddieangel8-Aug-12 13:24
eddieangel8-Aug-12 13:24 
GeneralRe: Get Variable Type from String Pin
SledgeHammer018-Aug-12 13:44
SledgeHammer018-Aug-12 13:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.