Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
C# - Create object from a string?
Message
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
VB 9.0
OS:
Windows XP SP2
Network:
Windows 2003 Server
Database:
MS SQL Server
Divers
Thread ID:
01308851
Message ID:
01309063
Vues:
7
Viv,

You are correct in the sense that I'm assuming everything will be a string. I agree that a more robust solution would be to use the anonymous type syntax that is available in .NET 3.0, and return those values in there native type, however this works for what Rodd was asking.

Kurt

>>Rodd,
>>
>>You can create a static method to parse the string for you. Here's a simple console application that demonstrates it. I'm assuming the fname and lname parameters could be passed in any order which is why I'm looping through the array to match the parameter names. There are probably other ways to do the same thing such as using a delegate, however I find this approach the easiest to follow.
>>
>>
>>using System;
>>
>>class Program
>>{
>>	static void Main(string[] args)
>>	{
>>		string myString = "{fname='Joe', lname='Smith'}";
>>
>>		Person SomePerson = new Person(myString);
>>
>>		Console.WriteLine(SomePerson.FirstName);
>>		Console.WriteLine(SomePerson.LastName);
>>		Console.WriteLine("Press any key");
>>		Console.ReadKey();
>>	}
>>}
>>
>>public class Person
>>{
>>	public string LastName { get; set; }
>>	public string FirstName { get; set; }
>>	public Person(string tString)
>>	{
>>		//	Remove the {}' characters so that the string looks
>>		//	like "fname=Joe, lname=Smith"
>>		tString = ((tString.Replace("{", "")).Replace("}", "")).Replace("'", "");
>>
>>		FirstName = MyMethods.ParseString("fname", tString, ',');
>>		LastName = MyMethods.ParseString("lname", tString, ',');
>>	}
>>}
>>public class MyMethods
>>{
>>	public static string ParseString(string ParamName, string StringToParse, char Delimiter)
>>	{
>>		string[] NameValues;
>>		string[] KeyValues = StringToParse.Trim().Split(Delimiter);
>>
>>		foreach (string oString in KeyValues)
>>		{
>>			NameValues = oString.Trim().Split('=');
>>
>>			if (NameValues[0] == ParamName)
>>				return NameValues[1];
>>		}
>>		return "";
>>	}
>>}
>>
>>
>
>Hi,
>Haven't you lost an anonymous type somewhere (s)
>Best,
>Viv
>
>
>>Hope this helps,
>>Kurt
>>>I'm very new to C# so please forgive me if this is a really simple question.
>>>
>>>Given the following code:
>>>
>>>string myString = "{fname='Joe', lname='Smith'}";
>>>
>>>
>>>Is there any way to use myString to actually instantiate an object with two properties (fname and lname)?
>>>
>>>I know I can do this:
>>>
>>>var myObject = new {fname='Joe', lname='Smith'};
>>>
>>>
>>>So I guess what I'm asking is, is there a way to dynamically use myString something like this: (I know this doesn't work but its here to give an idea of what I'm wanting to do)
>>>
>>>var myObject = new (myString);
>>>
>>>
>>>Thanks for your help!
>>>
>>>Rodd
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform