-1

How do I convert "Account" in to

<Account>

or "Object Name" into

<Object with matching name>

I'm attempting to parse a string and replace values in a string with object properties.

This is similar to what we do with form letters we send out, you know the Dear [Customer Name], thank you for purchasing [Some Item]. In this case the fields in the letter aren't setup by me and I need a means of converting what is in the text block into an object property.

Currently I'm using a code in the string {value=x; id=y ;property=z} and running that through a switch case to convert it into an object. Then replacing the {} with the object properties.

for example "some random string {value=1; id=1; property=Name} continued random string"

I parse the string to locate {value=, on a hit it runs through a switch case, where on case 1: return Account(ID = 1). The I grab Account.Name

Once I have that I put it in a text box so the user can validate that it is correct before generating a final document to be sent out.

Is there a way to have {Object.Property} in the string then use reflection to convert that string value to the object value?

Get property value from string using reflection

I used the above in other instances, but that requires that I have the object.

  • I've removed your ASP.NET tag as there isn't anything related to ASP.NET in your question content. I'm wondering why you're trying to reinvent the wheel and make a poor man's JSON? – ProgrammingLlama Aug 28 '21 at 13:49
  • Can you use JSON to do this? blah blah blah {Account.Name} blah blah blah. I've used it to grab an object but not replacing sections from a string. It could also be {Person.Name} or {Car.Name}. I'm not familiar with a way to do this with JSON – Robert Sullivan Aug 28 '21 at 13:52
  • Can you please decide for one pattern that you use? You posted: [token], {token} , {C#_object} and "token". Looks like you should use regular expressions to match your pattern and to return the index(es) of any match(es). – BionicCode Aug 28 '21 at 14:28
  • Thank you Oliver for continuing to make SO the place it is.... – Robert Sullivan Aug 28 '21 at 14:43
  • @OlivierRogier would be cool if knowing the answer was a prerequisite for downvoting it. – Robert Sullivan Aug 28 '21 at 14:49

1 Answers1

-1
public static List<string> GetClassProperties(string className, IEnumerable<string> propertiesToExclude = null)
        {
            Type theType = Type.GetType(className);
            if (theType == null)
            {
                return null;
            }
}

This'll get you the Object type, you do need the full namespace for it to work, can't just be "Account" would need to be Project.Models.Account

  • This is not about reflection but parsing strings! – BionicCode Aug 29 '21 at 10:54
  • Also the null check is totally redundant if you return null anyway. – BionicCode Aug 29 '21 at 10:55
  • I'm reasonably certain you don't understand what I'm asking. I was asking how to convert "Account" into the object account. That's why the question was tagged with reflection originally – Robert Sullivan Aug 29 '21 at 13:42
  • It's reasonably certain that you don't understand what reflection is. How does reflection help you to create instances of an object and initialize it with a value you extract from a string? – BionicCode Aug 29 '21 at 13:51
  • There is a strong mapping between string values and onjects. The easiest is to use JSON as it is easily editable even using a text editor and you can directly create instances of the related C# object from it using serialization. You don't need reflection for this job, believe me. – BionicCode Aug 29 '21 at 13:55
  • If your string contains strong type information consider to use XML, which also supports deserialization into plain C# objects. – BionicCode Aug 29 '21 at 14:07
  • @BionicCode The users are able to create template forms, they can add in data from objects in the database. The users create a string that has their text then {value= x} which I parse when I want to send the form. I convert their input into the values of the object in the database. For example the users could create a form that has Hello [Person Name]. I parse the text to find [Person Name] then I convert "Person Name" into the database object Person.Name. How do you downvote when I figured out how to do what I wanted to do... This is why people hate asking questions on SO. – Robert Sullivan Aug 29 '21 at 18:29
  • I did not downvote your answer. I did downvote your question as it lacks some crucial details in order to provide a useful answer. – BionicCode Aug 29 '21 at 18:40