Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Getting 'column' properties
Message
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01619178
Message ID:
01619189
Vues:
41
Hi Rick,

We actually do use AutoMapper already in our application, I just didn't think I can use it for what I was looking for. I started writing the code yesterday although probably quite roundabout way of doing things:
List<Tuple<String,Int32>> startingList = new List<Tuple<String, Int32>>();
            List<String> finalList = new List<String>();             

            foreach (PropertyInfo p in typeof(SalesPointsLicensesList).GetProperties())
            {
                propertyName = p.Name;
                propertyType = p.PropertyType;
                if (propertyType==typeof(Boolean))
                {
                    licenseCount = ClientLicenses.GetLicenseCount(propertyName);
                    if (licenseCount > 0)
                    {
                        startingList.Add(new Tuple<String, Int32>(propertyName, licenseCount));
                    }
                    else
                    {
                        finalList.Add(propertyName);
                    }
                }
            }

            foreach (var salespointLicense in searchViewModel.Result.List)
            {
                    // Here I will first iterate startingList object then finalList and add new LicenseInfo object to the List of Licenses

            }
(I've added these to my partial class):
public partial class LicenseInfo
    {   
        public String LicenseType {get; set;}
        public Boolean Activated {get; set;}      
        public Int32 TotalAllowed {get; set;}
    }

    public partial class SalesPointsLicensesList
    {
        public List<LicenseInfo> Licenses { get; set;}
    }
The whole thing I am doing in order to generate table on the client and I was thinking that iterating the object of known structure will be much easier.



>There are a number of ways to do this.
>
>Use a tool like Automapper, which will let you map properties of an object to another. It can create new objects or update existing objects.
>
>For simpler things I use some helper functions like this CopyObjectData() (part of the westwind.utilities NuGet package):
>
>https://github.com/RickStrahl/WestwindToolkit/blob/master/Westwind.Utilities/Utilities/DataUtils.cs#L241
>
>which uses Reflection to generically copy properties from one object to another.
>
>Note this does shallow copies meaning it just copies top level properties/values. You can exclude properties (to allow for overwriting values that you don't want updated like child objects perhaps or values that don't come in from the source object). This works fairly well for most entity objects.
>
>Automapper is the more sophisticated solution but it requires more setup and configuration.
>
>
>+++ Rick ---
>
>>Hi everybody,
>>
>>I have a model class which is modeled after a view using Reverse POCO Generator. I am returning that view back. My question is - how can I get back to the properties of each column (e.g. for example, public String Salespoint {get;set;} - I need to get 'Salespoint' back)?
>>
>>In other words, I started this code
>>
>>
>> foreach (var col in searchViewModel.Result.List) // List is IEnumerable of SalesPointsLicensesList class
>>            {
>>                
>>            }
>>
>>I need to know the name of the column (class property) and its value. How can I get the name of the property here?
>>
>>Thanks in advance.
>>
>>UPDATE. Looks like I would need to use reflection and PropertyInfo class.
If it's not broken, fix it until it is.


My Blog
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform