Click here to Skip to main content
14,453,600 members
   

C#

 
QuestionCAS REST API with C' shared login Ticket with Internet explorer or Edge Pin
MeAndCode23-Jan-20 4:19
MemberMeAndCode23-Jan-20 4:19 
QuestionDialogfenster auf- und zuklappen (Open and close dialog window) Pin
CopWorker23-Jan-20 1:36
MemberCopWorker23-Jan-20 1:36 
AnswerRe: Dialogfenster auf- und zuklappen (Open and close dialog window) Pin
Gerry Schmitz23-Jan-20 5:11
mveGerry Schmitz23-Jan-20 5:11 
QuestionGet Data From Deserialized List By Type Pin
Kevin Marois22-Jan-20 9:45
professionalKevin Marois22-Jan-20 9:45 
AnswerRe: Get Data From Deserialized List By Type Pin
Richard Deeming22-Jan-20 10:01
communityengineerRichard Deeming22-Jan-20 10:01 
GeneralRe: Get Data From Deserialized List By Type Pin
Kevin Marois22-Jan-20 10:15
professionalKevin Marois22-Jan-20 10:15 
GeneralRe: Get Data From Deserialized List By Type Pin
Kevin Marois22-Jan-20 12:12
professionalKevin Marois22-Jan-20 12:12 
GeneralRe: Get Data From Deserialized List By Type Pin
Richard Deeming23-Jan-20 1:48
communityengineerRichard Deeming23-Jan-20 1:48 
Something like this should work:
public class Parser<T> where T : EntityBase
{
    public List<T> Data { get; set; }
    
    public void LoadDataFromFile(string fileName)
    {
        List<RuleGroupEntity> groups = Serialization.DeSerializeObject<List<RuleGroupEntity>>(fileName);
        switch (typeof(T))
        {
            case typeof(RuleGroupEntity):
            {
                Data = (List<T>)(object)groups;
                break;
            }
            case typeof(RuleConfigurationEntity):
            {
                List<RuleConfigurationEntity> configs = groups.SelectMany(g => g.RuleConfigurations).ToList();
                Data = (List<T>)(object)configs;
                break;
            }
            case typeof(RuleDefinitionEntity):
            {
                List<RuleDefinitionEntity> definitions = groups.SelectMany(g => g.RuleConfigurations).Select(c => c.RuleDefinition).ToList();
                Data = (List<T>)(object)definitions;
                break;
            }
            default:
            {
                throw new NotSupportedException($"Type '{typeof(T)}' is not supported.");
            }
        }
    }
}
However, I'm never entirely convinced by a generic type which switches on the type parameter. Perhaps you could use specific parser types instead?
public abstract class Parser<T> where T : EntityBase
{
    public List<T> Data { get; set; }
    
    public void LoadDataFromFile(string fileName)
    {
        List<RuleGroupEntity> groups = Serialization.DeSerializeObject<List<RuleGroupEntity>>(fileName);
        LoadDataFromGroups(groups);
    }
    
    protected abstract void LoadDataFromGroups(List<RuleGroupEntity> groups);
}

public class RuleGroupEntityParser : Parser<RuleGroupEntity>
{
    protected override void LoadDataFromGroups(List<RuleGroupEntity> groups)
    {
        Data = groups;
    }
}

public class RuleConfigurationEntityParser : Parser<RuleConfigurationEntity>
{
    protected override void LoadDataFromGroups(List<RuleGroupEntity> groups)
    {
        Data = groups.SelectMany(g => g.RuleConfigurations).ToList();
    }
}

public class RuleDefinitionEntityParser : Parser<RuleDefinitionEntity>
{
    protected override void LoadDataFromGroups(List<RuleGroupEntity> groups)
    {
        Data = groups.SelectMany(g => g.RuleConfigurations).Select(c => c.RuleDefinition).ToList();
    }
}




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

GeneralRe: Get Data From Deserialized List By Type Pin
Kevin Marois23-Jan-20 8:35
professionalKevin Marois23-Jan-20 8:35 
GeneralRe: Get Data From Deserialized List By Type Pin
Richard Deeming23-Jan-20 10:34
communityengineerRichard Deeming23-Jan-20 10:34 
GeneralRe: Get Data From Deserialized List By Type Pin
Kevin Marois23-Jan-20 8:51
professionalKevin Marois23-Jan-20 8:51 
AnswerRe: Get Data From Deserialized List By Type Pin
BillWoodruff22-Jan-20 22:44
mveBillWoodruff22-Jan-20 22:44 
AnswerRe: Get Data From Deserialized List By Type Pin
Gerry Schmitz23-Jan-20 5:14
mveGerry Schmitz23-Jan-20 5:14 
QuestionException thrown: 'System.IO.FileNotFoundException' sqlite Pin
Member 1472399222-Jan-20 9:41
MemberMember 1472399222-Jan-20 9:41 
AnswerRe: Exception thrown: 'System.IO.FileNotFoundException' sqlite Pin
Richard Deeming22-Jan-20 9:54
communityengineerRichard Deeming22-Jan-20 9:54 
Questionhow flip the canvas vertically ? Pin
Le@rner22-Jan-20 2:56
MemberLe@rner22-Jan-20 2:56 
AnswerRe: how flip the canvas vertically ? Pin
OriginalGriff22-Jan-20 3:29
communityengineerOriginalGriff22-Jan-20 3:29 
GeneralRe: how flip the canvas vertically ? Pin
Le@rner22-Jan-20 20:19
MemberLe@rner22-Jan-20 20:19 
AnswerRe: how flip the canvas vertically ? Pin
Gerry Schmitz22-Jan-20 8:26
mveGerry Schmitz22-Jan-20 8:26 
QuestionAsp.net Pin
RajaMohammed.A22-Jan-20 2:35
MemberRajaMohammed.A22-Jan-20 2:35 
AnswerRe: Asp.net Pin
OriginalGriff22-Jan-20 3:29
communityengineerOriginalGriff22-Jan-20 3:29 
AnswerRe: Asp.net Pin
Richard Deeming22-Jan-20 4:11
communityengineerRichard Deeming22-Jan-20 4:11 
AnswerRe: Asp.net Pin
Gerry Schmitz22-Jan-20 8:37
mveGerry Schmitz22-Jan-20 8:37 
QuestionConvert the data intered by the user to integer number Pin
Member 906313820-Jan-20 23:37
MemberMember 906313820-Jan-20 23:37 
AnswerRe: Convert the data intered by the user to integer number Pin
OriginalGriff20-Jan-20 23:47
communityengineerOriginalGriff20-Jan-20 23:47 

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.