1 year ago

#385451

test-img

Learner

Using generic method and generic object for generating different cover type values

I have a generic method for generating different insurance cover types. CoverType is a generic object.

public class CoverType<T>
{
    public Func<T, string> Values { get; set; }
    public string Name { get; set; }
    public string Asset { get; set; }
}

public interface IGenerateCoverTypeService
{
    Stream GenerateCoverType<T>(CoverType<T> coverType, IEnumerable<T> coverValues);
}

public Stream GenerateCoverType<T>(CoverType<T> coverType, IEnumerable<T> coverValues)
{
    if(string.IsNullOrEMpty(coverType.Name))
        return null;
    
    var sbCoverValues = new StringBuilder();
    sbCoverValues.AppendLine(coverType.Name);
    foreach(var values in coverValues)
        sbCoverValues.AppendLine(coverType.Values(value));
    return new MemoryStream(Encoding.UTF8.GetBytes(sbCoverValues.ToString));
}

I am trying to get the insurance cover types values stream in the Motor Insurance page using the below method. MotorInsurance.cs

public Func<CoverType<MotorInsuranceCover>> GetMotorCoverTypeDefaults { get; set; }
.
.
private async Task GetMotorCoverDetails()
{
    var coverTypeDefaults = GetMotorCoverTypeDefaults();
    var ms = coverTypeService.GenerateCoverType(coverTypeDefaults, coverValues);
    .
    .
}

MotorInsurance.cs pass CoverType<MotorInsuranceCover> motorcovertype as input

Name:"Motor Cover",
Values: of type CoverType<MotorInsuranceCover>

MotorInsuranceCover object has the following values

Asset:"Car"
ID:"MERC25BT1521RN02015A"
Desc:"2015 MERCEDES-BENZ C250 BLUETEC 205 "
PriceNew:45500
PriceRetail:35000
Year:"2015"
Make:"MER"
Type:"4D SEDAN"

The expected output is a stream of string values which is an input to a print function.

The above function is not working and I am not getting expected result stream when a motor cover type is passed.

The error I am getting is - CoverType requires 1 type arguments

Can anyone help.

c#

generics

memorystream

0 Answers

Your Answer

Accepted video resources