2

I try to do a generic function of this

    networking.Ssl_Elts = (from line in allLines
                           let data = line.Split(',')
                           where line.Substring(0, Len("SSL_ELT,")) == "SSL_ELT,"
                           select new SSL_ELT(data)
                           ).ToList();

    networking.Ssl_Headers = (from line in allLines
                              let data = line.Split(',')
                              where line.Substring(0, Len("SSL_HEADER,")) == "SSL_HEADER,"
                              select new SSL_HEADER(data)
                              ).ToList(); 

By doing this :

public List<T>GetObjects<T> (IEnumerable<string> allLines,string ObjectName)
{
    var result =  (from line in allLines
            let data = line.Split(',')
            where line.Substring(0, Len("SSL_HEADER,")) == "SSL_HEADER,"
            select new T(data)).ToList();
    return (T)result;
}

So I would be able to call this function passing only :

networking.Ssl_Headers = GetObjects<Ssl_Headers>(allLines, "SSL_HEADER");

But it doesn't work.

Any idea.

Thanks in advance

Stéphan F
  • 85
  • 1
  • 9
  • what exactly does "not work" mean? Any exception? Unexpected behaviour? What is `Ssl_Headers`? – MakePeaceGreatAgain Mar 25 '21 at 09:34
  • Btw. your method should return a **list** of `T`. However you cast the result-list to `T`. I suppose you can just omit the cast, as `result`already **is** a `List`. – MakePeaceGreatAgain Mar 25 '21 at 09:37
  • 1
    @HimBromBeere `select new T(data)` wouldn't work as `T` is not known to contain a constructor accepting `data`. – GSerg Mar 25 '21 at 09:37
  • @GSerg That´s the most *obvious* one, but as you see in my comments there are many more including those we don´t even know as OP isn´t providing those information. – MakePeaceGreatAgain Mar 25 '21 at 09:38
  • Does this answer your question? [Generic method: instantiate a generic type with an argument](https://stackoverflow.com/questions/3327596/generic-method-instantiate-a-generic-type-with-an-argument) – GSerg Mar 25 '21 at 09:39
  • 3
    `line.Substring(0, Len("SSL_HEADER,")) == "SSL_HEADER,"` is a [`String.StartsWith`](https://learn.microsoft.com/en-us/dotnet/api/system.string.startswith?view=net-5.0) Method – Drag and Drop Mar 25 '21 at 09:40

1 Answers1

4

You can't call new T(data), because not every type has a constructor accepting a single argument of type string[], however, you could use a factory function:

public List<T> GetObjects<T>(IEnumerable<string> allLines, string objectName,
    Func<string[], T> factory)
{
    objectName += ",";
    return (from line in allLines
        let data = line.Split(',')
        where line.Substring(0, Len(objectName)) == objectName
        select factory(data)).ToList();
}

Which can be called like this:

networking.Ssl_Headers = GetObjects(allLines, "SSL_HEADER", x => new Ssl_Headers(x));

And, as mentioned in the comments by @DragAndDrop, using string.StartsWith would be simpler:

public List<T> GetObjects<T>(IEnumerable<string> allLines, string objectName,
    Func<string[], T> factory)
{
    return (from line in allLines
        let data = line.Split(',')
        where line.StartsWith(objectName + ",")
        select factory(data)).ToList();
}
Johnathan Barclay
  • 18,599
  • 1
  • 22
  • 35