Access properties of model on Generic method


Scenario:

We want to edit model before further processing. The model is passed to a Generic method. Also name of Datamember of the model has to be different than property name.

Solution:


  1.  1
     2
     3
     4
     5
     6
     7
     8
     9
    10
            public interface IModel
            {
                string Filename { get; set; }
            }
    
            public class Model : IModel
            {
                [DataMember(Name = "fileName")]
                public string FileName { get; set; }
            }


  2.  
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
        public abstract class Client<T> : IClient where T : class
        {
            private void Process(T model)
            {
                if (model is IModel model)
                {
                    model.Filename = application.FileName;
                }
            }
        }

No comments:

Post a Comment

Move Github Sub Repository back to main repo

 -- delete .gitmodules git rm --cached MyProject/Core git commit -m 'Remove myproject_core submodule' rm -rf MyProject/Core git remo...