P.Brian.Mackey
P.Brian.Mackey

Reputation: 44295

Is there any way to determine an unknown Type?

In certain cases I need to determine a type and I just don't know what the type is. For example, DevExpress plugins request a type when getting data.

e.Data.GetDataPresent(typeof(DataRow))

I set the data source as a DataTable. I don't know if the data present is actually a String, DataRow, DataColumn or other.

Is there any way to determine this type without having to hack at all the possibilties one by one in the debugger/Immediate window?

UPDATE
In this case it's a winform app event

private void grid_VDragOver(object sender, System.Windows.Forms.DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(DataColumn)))//DataColumn is just a guess..no idea
                e.Effect = DragDropEffects.Move;
            else
                e.Effect = DragDropEffects.None;
        }

Upvotes: 1

Views: 264

Answers (2)

Oded
Oded

Reputation: 499382

You can use object.GetType() - all objects implement it, so you can simply use that:

e.Data.GetDataPresent(variable.GetType())

Update:

With the revision (drag drop data accessible only through IDataObject and no variable), this answer is of limited use to the OP. I will keep it here for those whom it might help that do not have these exact constraints.

Upvotes: 2

KeithS
KeithS

Reputation: 71591

If you have a variable of unknown type, you can call GetType() on that variable (the method is common to all Objects) and you will get a Type instance representing the most derived type of the variable.

In this case, it sounds like you are trying to ask this DataObject whether data is available in some form that can be represented as the type you are passing in. In that case, you should know what you want to work with (strings, numbers, DateTimes etc) and you should specify that. If you don't know what you want out of the DataObject, then it's not going to be very helpful.

Upvotes: 2

Related Questions