Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / April 2006

Tip: Looking for answers? Try searching our database.

system.reflection and COM objects

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Doudou - 31 Mar 2006 08:43 GMT
Hello,
First, I'm french so my english isn't very good.

I must import MsProject 2000 tasks into dataTable.
I would like to use "system.reflection" (or some thing like that) to
write generic code in order to call each property of COM MsProject Task
object.

But it seems that system.reflection works only for .NET managed
objects. I tried to find other possibilities but I found nothing.

Is someone can help me ?
Thanks in advance.

Steph
VB6 To .NET - 31 Mar 2006 14:47 GMT
Here is some generic reflection code that we use.  The methods do what they
say.  There is some logic in there specific to scenarios we've ran across in
our projects, it works pretty well.  Good luck.

Public Shared Function GetDefaultValue(ByVal voObject As Object) As Object

Dim voReturnVal As Object

Try

'Use reflection to get value of default property for a given object.

If voObject Is Nothing _

OrElse voObject.GetType.IsPrimitive _

OrElse voObject.GetType.Equals(Type.GetType("System.String")) _

OrElse voObject.GetType.Equals(Type.GetType("System.DateTime")) _

OrElse voObject.GetType.Equals(Type.GetType("System.Decimal")) _

OrElse voObject.GetType.Equals(Type.GetType("System.DBNull")) Then

'No default property. Just return value passed in.

voReturnVal = voObject

ElseIf voObject.GetType.Equals(Type.GetType("System.Guid")) Then

If voObject.ToString = String.Empty Then

voReturnVal = System.DBNull.Value

Else

voReturnVal = voObject.ToString

End If

Else

'Get default property value.

voReturnVal = voObject.GetType.InvokeMember(String.Empty, _

BindingFlags.Default Or BindingFlags.Public Or _

BindingFlags.IgnoreCase Or BindingFlags.Instance Or _

BindingFlags.GetProperty, _

Nothing, _

voObject, _

Nothing)

If voReturnVal.GetType.ToString = "System.__ComObject" Then

voReturnVal = GetDefaultValue(voReturnVal)

End If

End If

Return voReturnVal

Catch ex As System.MissingMethodException

Return voObject

Catch ex As System.Reflection.TargetInvocationException

Return voObject

Finally

voReturnVal = Nothing

End Try

End Function

Public Overloads Shared Function GetPropertyValue(ByVal voObject As Object,
_

ByVal vsProperty As String) As Object

Return voObject.GetType.InvokeMember(name:=vsProperty, _

invokeAttr:=BindingFlags.GetProperty, _

binder:=Nothing, _

target:=voObject, _

args:=Nothing)

End Function

Public Overloads Shared Function GetPropertyValue(ByVal voObject As Object,
_

ByVal vsProperty As String, _

ByVal viIndex As Integer) As Object

voObject = GetPropertyValue(voObject, vsProperty)

Return voObject(viIndex)

End Function

Public Shared Sub PutDefaultValue(ByRef roObject As Object, _

ByVal voValue As Object)

Try

If Not IsNothing(roObject) AndAlso Marshal.IsComObject(roObject) Then

Call roObject.GetType.InvokeMember(name:=String.Empty, _

invokeAttr:=BindingFlags.Default Or BindingFlags.Public Or _

BindingFlags.IgnoreCase Or BindingFlags.Instance Or _

BindingFlags.SetProperty, _

binder:=Nothing, _

target:=roObject, _

args:=New Object() {voValue})

Else

roObject = voValue

End If

Catch ex As System.Reflection.TargetInvocationException

'Eat the exception. There is no default value to put back.

End Try

End Sub

Public Shared Sub PutPropertyValue(ByRef roObject As Object, _

ByVal vsProperty As String, _

ByVal voValue As Object)

Call roObject.GetType.InvokeMember(name:=vsProperty, _

invokeAttr:=BindingFlags.SetProperty, _

binder:=Nothing, _

target:=roObject, _

args:=New Object() {voValue})

End Sub

Public Overloads Shared Function GetFieldValue(ByVal voObject As Object, _

ByVal vsField As String) As Object

Return voObject.GetType.InvokeMember(name:=vsField, _

invokeAttr:=BindingFlags.GetField, _

binder:=Nothing, _

target:=voObject, _

args:=Nothing)

End Function
Doudou - 03 Apr 2006 10:08 GMT
Thanks, it helps me but my first problem is that I would loop on each
property of MsProject task object which have a lot of properties.
So I would like to find a method to list "dynamically" all properties
of a task object and after, I can use your functions.
VB6 To .NET - 03 Apr 2006 14:57 GMT
Can't help you there.  Maybe someone else can jump in and show a way to
dynamically get all objects of a class.  It's beyond me though.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.