Click here to Skip to main content
15,907,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends,
I have model say
SQL
 public class GenericPaymentMethod<T>
    {
        public int TenantID { get; set; }
        public string Market { get; set; }
        public List<T> CreditCard { get; set; }
        public List<T> DebitCard { get; set; }
        public List<T> BPay { get; set; }
    }
public class EditPaymentMethod
    {
        public GenericPaymentMethod<Multicheckbox> MyProperty { get; set; }
    }
public class Multicheckbox
    {
        public string CheckboxName { get; set; }
        public bool IsChecked { get; set; }
    }

I'm creating mock data as follows
XML
public static EditPaymentMethod GetPaymentMethodDetails(int tenantID) {
            dynamic obj = new EditPaymentMethod();
            obj.MyProperty = new GenericPaymentMethod<Multicheckbox>();

            obj.MyProperty.TenantID = 1000;

            obj.MyProperty.Market = "AU";

            obj.MyProperty.CreditCard = new List<Multicheckbox>(){
                new Multicheckbox { CheckboxName = "Amex", IsChecked = true },
                new Multicheckbox { CheckboxName = "Master Card", IsChecked = false },
                new Multicheckbox { CheckboxName = "Visa", IsChecked = true }
            };

            obj.MyProperty.DebitCard = new List<Multicheckbox>(){
                new Multicheckbox { CheckboxName = "Amex", IsChecked = false },
                new Multicheckbox { CheckboxName = "Master Card", IsChecked = true },
                new Multicheckbox { CheckboxName = "Visa", IsChecked = false }
            };

            obj.MyProperty.BPay = new List<Multicheckbox>(){
                new Multicheckbox { CheckboxName = "Savings Cheque", IsChecked = true },
                new Multicheckbox { CheckboxName = "Credit Card", IsChecked = false },
            };

            for (int i = 0; i < obj.MyProperty.BPay.Count; i++)
            {
                obj.BillerCodes = new List<string> { "AAA", "BBB" };
            }

            return obj;
        }

I want to dynamically add properties to the object based on the no of item present in the BPay.

Any idea how to go about it?

Thanks in advance
Posted
Comments
BillWoodruff 10-Nov-14 8:08am    
I can't understand what it is you want to create in the instance of the 'EditPaymentClass named 'obj. Okay, you make 'obj dynamic: that just allows any instance of any Type to be assigned to 'obj. It doesn't mean that 'obj becomes extensible !

In the loop where you iterate over the contents of 'obj.MyProperty.BPay : what is it you wish to create in each iteration: surely not the same thing every time ?

What Type is 'BillerCodes: a struct, a class ?

Try to write a clear summary of why you need to add objects/Types at run-time that are not defined in the Classes you use here.

1 solution

Dynamic allows loosely-typed access to strongly-typed objects.

You should use the ExpandoObject class, which allows loosely-typed access to an internal dictionary:

dynamic configObject = new ExpandoObject();

configObject.Git = new GitCheckout {
Repository = config.Github.Url
};
 
Share this answer
 
Comments
dhage.prashant01 10-Nov-14 7:25am    
But i have model EditPaymentMethod, just need to add new properties depending on BPay.
Using:
dynamic configObject = new ExpandoObject();

There is no need for me to create EditPaymentMethod

Correct me if I'm wrong.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900