Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you auto-generate code for assigning properties of an object in C#? (Visual Studio)

Example: I am working with an API that has definitions for various objects with many properties.

This object has about 40 properties, and I'd like to set them all.

Is there a way to auto-generate the following code from an object?

contact.AddressId = null;
contact.Anniversary = null;
contact.AssistantId = null;
contact.BirthDay = null;
contact.Children = null;
contact.CompanyAddressInfo = null;
contact.CompanyIdentifier = null;
contact.DisablePortalLogin = null;
contact.Email = null;
contact.Emails = null;
contact.ExtensionData = null;
contact.Fax = null;
contact.Faxes = null;
contact.FaxExt = null;
contact.FirstName = null;
contact.Gender = null;
contact.Id = null;
contact.Inactive = null;
contact.LastName = null;
contact.LastUpdated = null;
contact.ManagerId = null;
contact.Married = null;
contact.NickName = null;
contact.PersonalAddress = null;
contact.PersonalAddressFlag = null;
contact.Phone = null;
contact.PhoneExt = null;
contact.Phones = null;
contact.PortalPassword = null;
contact.PortalSecurityLevel = null;
contact.Relationship = null;
contact.School = null;
contact.SID = null;
contact.SignificantOther = null;
contact.SiteName = null;
contact.Title = null;
contact.Type = null;
contact.UnsubscribeFlag = null;
contact.UpdatedBy = null;

The purpose is for visualization and for me to see what I am and am not setting in my code.

Edited to be more specific.

like image 365
Sonny Childs Avatar asked Oct 28 '25 06:10

Sonny Childs


1 Answers

Try this

    static void Main(string[] args)
    {
        Contact obj = new Contact();
        var props = obj.GetType().GetProperties();

        foreach (var p in props)
            Console.WriteLine("contact.{0}= null;", p.Name);
        Thread.Sleep(1000);
    }

Then copy paste the console screen or write it to a file instead.

like image 192
Jonathan Nappee Avatar answered Oct 29 '25 21:10

Jonathan Nappee