Is there a jQuery extension in C #?

 var _Contact = new ContactLstModel { 
             ContactName="xxxxxx",
              EmailAddr="yyyyyy",
               ContactNo="ddddddd",
               SelectedContactType="dddd"
            };

 var _ContactOption= new ContactLstModel{
                 ContactType= new List<SelectListItem>(){
                 new SelectListItem{
                  Text="sss",Value="ddd"
                 }
                 }
            };

as you can see, both have the same model ContactLstModel. Now how to combine both into one?

As in jQuery, we have $.extend(dest,source);

Is there an equivalent in C #?

+3
source share
3 answers

In C # and .NET 4.5 there is no built-in equivalent of $ .extend.

However, you can find many examples of people trying to achieve this behavior using reflection.NET. There are others that use serialization (JSON.NET, etc.) to achieve similar behavior. Another approach would be to use IOC containers such as Automapper .

, , Automapper IOC:

var expr = Mapper.CreateMap<ContactLstModel, ContactLstModel>().ForMember("ContactType", (conf) => { conf.Ignore(); });
var merged = Mapper.Map<ContactLstModel, ContactLstModel>(_Contact, _ContactOption);

Automapper .

, Reflection.

, , CopyValues, , .

CopyValues<ContactLstModel>(_Contact, _ContactOption);

, ContactType .

CopyValues ​​ :

    public static void CopyValues<T>(T target, T source)
    {
        Type t = typeof(T);

        var properties = t.GetProperties().Where(prop => prop.CanRead && prop.CanWrite);

        foreach (var prop in properties)
        {
            var value = prop.GetValue(source, null);
            if (value != null)
                prop.SetValue(target, value, null);
        }
    }

, , jquery extend (, ..), . .

, # , Javascript, #, Javascript for-in Object.keys().

+6

:

public static class ContactModelExtensions {
    public static ContactModel Extend(this ContactModel first, ContactModel replacement) {
        if (!replacement.ContactsName.IsNullOrEmpty()) // or whatever criteria you want
        {
            first.ContactsName = replacement.ContactsName;
        }
        // similar assignment for all other properties

        return first; // since we return the first one, properties not set in override
                      // will be untouched
    }
}

var extendedContact = _Contact.Extend(_ContactOptions);

.

+3

. , ValueInjecter:

    public class NoNullsInjection : ConventionInjection
    {
        protected override bool Match(ConventionInfo c)
        {
            return c.SourceProp.Name == c.TargetProp.Name
                    && c.SourceProp.Value != null;
        }
    }

    class A
    {
        public string a { get; set; }
        public string b { get; set; }
    }

    static void Main(string[] args)
    {
        A a1 = new A() { a = "123" };
        A a2 = new A() { b = "456" };
        A c = new A();

        c.InjectFrom(new NoNullsInjection(),a1,a2); 
        // "c" now have a="123", b="456"     

    }
+2

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


All Articles