Click here to Skip to main content
16,013,918 members
Home / Discussions / C#
   

C#

 
AnswerRe: Component To Select Multiple Folders Pin
Wendelius3-Dec-08 10:57
mentorWendelius3-Dec-08 10:57 
GeneralRe: Component To Select Multiple Folders Pin
Michael Fritzius5-Dec-08 2:32
professionalMichael Fritzius5-Dec-08 2:32 
GeneralRe: Component To Select Multiple Folders Pin
Wendelius5-Dec-08 8:19
mentorWendelius5-Dec-08 8:19 
QuestionHow to convert dynamicaly an array of one type to an array of another type Pin
User 43300283-Dec-08 7:57
User 43300283-Dec-08 7:57 
AnswerRe: How to convert dynamicaly an array of one type to an array of another type Pin
Christian Graus3-Dec-08 9:07
protectorChristian Graus3-Dec-08 9:07 
GeneralRe: How to convert dynamicaly an array of one type to an array of another type Pin
User 66583-Dec-08 9:48
User 66583-Dec-08 9:48 
GeneralRe: How to convert dynamicaly an array of one type to an array of another type Pin
Christian Graus3-Dec-08 9:50
protectorChristian Graus3-Dec-08 9:50 
GeneralRe: How to convert dynamicaly an array of one type to an array of another type Pin
User 43300283-Dec-08 14:21
User 43300283-Dec-08 14:21 
You are almost right

Array.ConvertAll will convert the array, and all the objects included in the array will have the right type (t.GetElementType() in my example)

The problem is, that Array.ConvertAll returns a reference who is an object[], and, when i use this object as a parameter to a method, that gives me an InvalidCastException because it cannot convert from object[] to myType[], i mean, if you want to convert to string[] you may just write

string[] test = (string[]) Array.ConvertAll....

But here the type is given dynamicaly

I fixed it using a not very good strategy, but it works, here is the code:
public object ConvertTo(Type t, object value) {
    if (t == null) {
        return value;
    }
    if (t.IsEnum) {
        return Enum.Parse(t, value.ToString());
    }
    if (t.IsArray) {
        object[] _valArr;
        if (value.GetType().IsArray)
            _valArr = value as object[];
        else
            _valArr = new object[] { value };

        Array ret = Array.CreateInstance(t.GetElementType(), _valArr.Length);
        Array.ConvertAll(_valArr, delegate(object o) { return Convert.ChangeType(o, t.GetElementType()); }).CopyTo(ret, 0);

        return ret;
    }
    return Convert.ChangeType(value, t);
}


Saludos!!

____Juan

QuestionPDF Viewer for Win Form App using C# Pin
Karmendra Suthar3-Dec-08 7:47
Karmendra Suthar3-Dec-08 7:47 
AnswerRe: PDF Viewer for Win Form App using C# Pin
Wendelius3-Dec-08 8:20
mentorWendelius3-Dec-08 8:20 
GeneralRe: PDF Viewer for Win Form App using C# Pin
Karmendra Suthar3-Dec-08 8:26
Karmendra Suthar3-Dec-08 8:26 
GeneralRe: PDF Viewer for Win Form App using C# Pin
Wendelius3-Dec-08 8:37
mentorWendelius3-Dec-08 8:37 
QuestionVS2008 / Northwind dataset problem Pin
mhrtg3-Dec-08 7:15
mhrtg3-Dec-08 7:15 
AnswerRe: VS2008 / Northwind dataset problem Pin
Wendelius3-Dec-08 8:17
mentorWendelius3-Dec-08 8:17 
GeneralRe: VS2008 / Northwind dataset problem Pin
mhrtg3-Dec-08 9:24
mhrtg3-Dec-08 9:24 
GeneralRe: VS2008 / Northwind dataset problem Pin
Wendelius3-Dec-08 10:04
mentorWendelius3-Dec-08 10:04 
GeneralRe: VS2008 / Northwind dataset problem Pin
mhrtg4-Dec-08 4:26
mhrtg4-Dec-08 4:26 
QuestionAbout PrintPreview Problem?-Plz respond about my query [modified] Pin
Roney3-Dec-08 5:59
Roney3-Dec-08 5:59 
QuestionReport Progress from a method being executed within a background worker. Pin
Matthew Edmondson3-Dec-08 5:53
Matthew Edmondson3-Dec-08 5:53 
AnswerRe: Report Progress from a method being executed within a background worker. Pin
Mark Salsbery3-Dec-08 6:15
Mark Salsbery3-Dec-08 6:15 
GeneralRe: Report Progress from a method being executed within a background worker. Pin
Luc Pattyn3-Dec-08 6:49
sitebuilderLuc Pattyn3-Dec-08 6:49 
AnswerRe: Report Progress from a method being executed within a background worker. Pin
User 43300283-Dec-08 14:33
User 43300283-Dec-08 14:33 
Questionwierd behaviour of compiler / visual studio Pin
Piratenwichtl20003-Dec-08 5:52
Piratenwichtl20003-Dec-08 5:52 
GeneralRe: wierd behaviour of compiler / visual studio [modified] Pin
Luc Pattyn3-Dec-08 6:44
sitebuilderLuc Pattyn3-Dec-08 6:44 
QuestionRe: wierd behaviour of compiler / visual studio Pin
led mike3-Dec-08 6:52
led mike3-Dec-08 6:52 

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.