Click here to Skip to main content
15,887,988 members
Home / Discussions / C#
   

C#

 
QuestionHelp needed to convert function to a generic function Pin
Jim Taylor17-May-07 3:11
Jim Taylor17-May-07 3:11 
AnswerRe: Help needed to convert function to a generic function Pin
Not Active17-May-07 4:12
mentorNot Active17-May-07 4:12 
GeneralRe: Help needed to convert function to a generic function Pin
Jim Taylor17-May-07 4:59
Jim Taylor17-May-07 4:59 
AnswerRe: Help needed to convert function to a generic function Pin
Judah Gabriel Himango17-May-07 5:11
sponsorJudah Gabriel Himango17-May-07 5:11 
GeneralRe: Help needed to convert function to a generic function Pin
Jim Taylor17-May-07 5:34
Jim Taylor17-May-07 5:34 
GeneralRe: Help needed to convert function to a generic function Pin
Not Active17-May-07 6:20
mentorNot Active17-May-07 6:20 
GeneralRe: Help needed to convert function to a generic function Pin
Judah Gabriel Himango17-May-07 7:15
sponsorJudah Gabriel Himango17-May-07 7:15 
AnswerRe: Help needed to convert function to a generic function Pin
Nissim Salomon20-May-07 0:19
Nissim Salomon20-May-07 0:19 
Hi
the following code use reflection in order to convert string items to value
types using the static function TryParse.


using System;
using System.Collections.Generic;
using System.Reflection;

namespace CMDLineTest
{
class Program
{
static List<t> CommaSeparatedStringToIEnumerable<t>(string s)
{
List<t> values = new List<t>();

Type targetConversionType = typeof(T);

if (targetConversionType.IsValueType)
{
Type targetConversionTypeOutParamType = Type.GetType(targetConversionType.FullName + @"&");

if(targetConversionTypeOutParamType == null)
throw new Exception("");

MethodInfo tryParseMethodInfo = targetConversionType.GetMethod("TryParse", new Type[] { typeof(string), targetConversionTypeOutParamType });

if(tryParseMethodInfo != null)
{
string[] stringsToConvert = s.Split(',');

T item = default(T);

object[] methodParams = new object[2];

foreach (string str in stringsToConvert)
{
methodParams[0] = str;
methodParams[1] = item;

if (((bool)tryParseMethodInfo.Invoke(null, methodParams)) == true)
values.Add((T)methodParams[1]);

}
}
}

return values;
}

static void Main(string[] args)
{
List<int> intList = CommaSeparatedStringToIEnumerable<int>("1,2,3,4,5");

foreach (int item in intList)
Console.WriteLine(item);
}
}
}
QuestionWhat are the Books on C#..?? Pin
IamPoojaa17-May-07 2:39
IamPoojaa17-May-07 2:39 
AnswerRe: What are the Books on C#..?? Pin
Sathesh Sakthivel17-May-07 2:51
Sathesh Sakthivel17-May-07 2:51 
GeneralRe: What are the Books on C#..?? Pin
IamPoojaa17-May-07 3:08
IamPoojaa17-May-07 3:08 
GeneralRe: What are the Books on C#..?? Pin
Sathesh Sakthivel17-May-07 3:12
Sathesh Sakthivel17-May-07 3:12 
AnswerRe: What are the Books on C#..?? Pin
Kevin McFarlane17-May-07 3:13
Kevin McFarlane17-May-07 3:13 
QuestionTAPI Multmedia transmission Pin
CallieClearence17-May-07 2:21
CallieClearence17-May-07 2:21 
QuestionDiff b/w Windows Control Library and Class Library Pin
Rahul8317-May-07 0:52
Rahul8317-May-07 0:52 
AnswerRe: Diff b/w Windows Control Library and Class Library Pin
Pete O'Hanlon17-May-07 1:54
mvePete O'Hanlon17-May-07 1:54 
QuestionHow to connect c# (2.0 Framework [Dotnet 2005]) desktop application with ms sql 2000 Pin
prince_ppy17-May-07 0:42
prince_ppy17-May-07 0:42 
AnswerRe: How to connect c# (2.0 Framework [Dotnet 2005]) desktop application with ms sql 2000 Pin
andre_swnpl17-May-07 0:44
andre_swnpl17-May-07 0:44 
GeneralRe: How to connect c# (2.0 Framework [Dotnet 2005]) desktop application with ms sql 2000 Pin
prince_ppy17-May-07 0:48
prince_ppy17-May-07 0:48 
GeneralRe: How to connect c# (2.0 Framework [Dotnet 2005]) desktop application with ms sql 2000 Pin
andre_swnpl17-May-07 0:56
andre_swnpl17-May-07 0:56 
AnswerRe: How to connect c# (2.0 Framework [Dotnet 2005]) desktop application with ms sql 2000 Pin
Christian Graus17-May-07 1:10
protectorChristian Graus17-May-07 1:10 
AnswerRe: How to connect c# (2.0 Framework [Dotnet 2005]) desktop application with ms sql 2000 Pin
Nouman Bhatti17-May-07 1:48
Nouman Bhatti17-May-07 1:48 
Questionwriting with different color in excel Pin
Ankit Aneja17-May-07 0:26
Ankit Aneja17-May-07 0:26 
AnswerRe: writing with different color in excel Pin
gauthee17-May-07 1:00
gauthee17-May-07 1:00 
AnswerRe: writing with different color in excel Pin
Manoj Kumar Rai17-May-07 2:47
professionalManoj Kumar Rai17-May-07 2:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.