C # General Lists

I have a class that is a map in a field in a database. The class only cares about the name of the field and its sister type .NET. The type can be a string, int, datetime, etc.

class Foo()
{
    string Name { get; set; }
    Type FooType { get; set; }
}

I have another class that inherits from Foo, which adds a property for the value. Right now, I am storing the value as an object and use the switch statement to enter the value based on the FooType base classes.

class FooWithStuff() : Foo
{
    object Value { get; set; }   
}

Is there a way to implement this using generics to ensure type safety for values?

Edit: I made the key requirement bold. When declaring a list of Foo, it needs a type. If I did this against user classes, I would create an interface and use this. However, here I use int, string, DateTime, etc. Int is a structure, a string is an object, so Foo <object> does not work for both.

+3
source share
6 answers

Define your class as follows:

class Foo<T> : IFoo
{

    public Foo(string name)
    {
        Name = name;
    }

    string Name { get; set; }
    T Value {get; set;}
    Type FooType { get { return typeof(T); } }
}

You can then define the IFoo interface as:

string Name { get; set; }
Type FooType { get; set; }

And declare the list as:

List<IFoo> list = new List<IFoo>();
+8
source
class Foo
{
    public string Name { get; set; }
    public Type Type { get; set; }
}

class Bar<T> : Foo
{
    public T Value { get; set; }

    public Bar()
    {
        base.Type = typeof( T );
    }
}
+9
source

Foo Foo, ...

class Foo<T>
{
    T Value {get; set;}
}

Foo<int> myFoo = new Foo<int>();
myFoo.Value = 7;
+2

, .

edit: . Foo FooWithStuff:

public interface IFoo
{
  string Name{get;set;}
  Type FooType{get;set;}
}

public class FooWithStuff<T>:IFoo
{
   T Value {get;set;}
}
+1

, , ..

public class Foo<T>
{
  public string Name {get;set;}
  public T Value {get;set;}
  public Type FooType
  {
     get
     {
       return typeof(T);
     }
  }
}

Note that with linq you can just extract the types you need from the list, so if you are just interested in string fields for some reason, you can ...

List<object> list = getAllmyFoos();
foreach(Foo<string> sfoo in list.OfType<Foo<string>>())
{
  ...blah
}

Edit: Added FooType.

0
source

I would recommend using 2 interfaces:

public interface IFoo
{
  string Name {get; }
  Type Type { get; }
  object Value {get; set;}
}

public interface IFoo<T> : IFoo
{
  T Value {get; set}
}

Then execute it:

public class Foo<T> : IFoo<T>
{
   private T value;
   public Foo(string name, T value)
   {
     this.name = name;
     this.value = value;
   }

   public string Name { get { return name; } }
   public Type Type { get { return typeof(T); } }
   public T Value
   {
      get { return value; }
      set { value = value; }
   }

   object IFoo.Value
   {
      get { return value; }
      set { value = (T)value; }  // can check type before
   }
}

This way you can easily use the IFoo interface in a non-general context.

-2
source

Source: https://habr.com/ru/post/1703465/


All Articles