Pergunta

Basic concept is I want to get a 'property' return from an input in a signature method. NOT the data behind the property. EG: If I select 'DEV' I want to get the POCO Class property called 'DEV' not data stored in a part of the POCO class.

The end goal is to make a SQL deploy script maker where the environments may have different named databases on each environment(believe me not my choice). In the example it is simple in the real world is not as easy as appending a string like 'QA' or 'UAT' to whatever name. Someone followed no naming rules at all and just named things randomly so I found and made a collection and will basically do text hunting find and replacing for it. I am making a POCO object that holds the different names. I realize I may have to normalize and change the object but I was curious if I could get away with find in the 'property' of a POCO class in a loop rather than having to do a 1, environment, name, 2, environment, name, etc.

Code:

namespace SQL_Deployer
{
    public class DatabaseListing
    {
        public string DatabaseName { get; set; }
        public string Dev { get; set; }
        public string DEMO { get; set; }
        public string QA { get; set; }
        public string UAT { get; set; }
        public string PROD { get; set; }
    }

    public class DatabaseListings
    {
        protected static List<DatabaseListing> DataBaseListings { get; set; }

        public DatabaseListings()
        {
            DataBaseListings = returnDatabaseListings();
        }

        private List<DatabaseListing> returnDatabaseListings()
        {
            return new List<DatabaseListing>{
                new DatabaseListing{DatabaseName = "Main", Dev="Main", DEMO = "MainDemo", QA="MainQA", UAT="MainUAT", PROD="MainPROD"},
                new DatabaseListing{DatabaseName = "Sub", Dev="Sub", DEMO = "SubDemo", QA="SubQA", UAT="SubUAT", PROD="SubPROD"}
            };
        }
    }
}

namespace SQL_Deployer
{
    public class SQL_Rewriter : DatabaseListings
    {
        private string _fileLocationSource;
        private string _fileLocationTarget;
        public string FileTextOriginal;
        public string FileTextChanged;

        public SQL_Rewriter(string aSourceFile, string aTargetFile)
        {
            _fileLocationSource = aSourceFile;
            _fileLocationTarget = aTargetFile;
        }

        public void GetTextFromFile()
        {
            if (_fileLocationSource != null)
                FileTextOriginal = File.ReadAllText(_fileLocationSource).ToUpper();
            else
                FileTextOriginal = "COULD NOT READ";
        }

        public void ChangeTextToNewFormat()
        {
            if (FileTextOriginal != null)
            {
                List<string> distinctEnvironments = new List<string> { "Dev", "DEMO", "QA", "UAT", "PROD" };

                Dictionary<string, string> names = new Dictionary<string, string>();

                distinctEnvironments.ForEach(x =>
                    names.Add( 
                        x,
                        DataBaseListings.FirstOrDefault(y => y.DatabaseName == "Review").x  // I want the 'property' extenstion of the object matching the 'distinctEnvironments' List I set above
                        )
                    );

                // I should now have a dictionary for my environment with each enviroment 'property' I could loop through to find and replace text in a file.

                foreach(var replace in names)
                {
                    FileTextOriginal.Replace(replace.Key, replace.Value);

                    //TODO Write out file for each envionrment to sub directory in main placement.
                }
            }
            else if (FileTextOriginal == "COULD NOT READ")
                FileTextChanged = "COULD NOT READ INPUT";
            else
                FileTextChanged = "NO VALID INPUT TO READ";
        }
    }
}
Foi útil?

Solução

Sure, using reflection:

var QAprop = typeof(DatabaseListing).GetProperty("QA");

or to loop through all properties:

foreach(var prop in typeof(DatabaseListing).GetProperties())
    string name = prop.Name;

From there you can get the property's type, attributes, etc.

You'd have to use reflection again to get the value:

distinctEnvironments.ForEach(x =>
    names.Add( 
        x,
        typeof(DatabaseListing).GetProperty(x)
                               .GetValue(DataBaseListings.FirstOrDefault(y => y.DatabaseName == "Review"), null) 
        )
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top