What is reflection?

  • Thread starter Thread starter idletask
  • Start date Start date
I

idletask

Can someone tell me what Reflection is? Specifically, what's the
System.Reflection class for? I know that seems like a silly question
to some people here...

The reason is that I use a function that prints a printdoc object
directly to a file. I tried getting my object to print to file
directly by setting the 'to file ' property, but it would never work.

Someone gave me code that works great for this purpose, and it's the
only part of my app that I dont fully understand.

Thanks
 
Can someone tell me what Reflection is? Specifically, what's the
System.Reflection class for? I know that seems like a silly question
to some people here...

System.Reflection is a namespace that contains many classes. The classes
in the System.Reflection namespace allow one to write very general
libraries that can manipulate types (i.e. get and set property values,
fields, call methods, instantiate types) without knowledge of the
specific types at compile-time.

It's called reflection because the types in the namespace reflect, or
represent, elements of the program itself. For example, a MethodInfo
object represents a method of an object.
The reason is that I use a function that prints a printdoc object
directly to a file. I tried getting my object to print to file
directly by setting the 'to file ' property, but it would never work.

Someone gave me code that works great for this purpose, and it's the
only part of my app that I dont fully understand.

It's hard to say what your code is doing if we can't see it.

-- Barry
 
Barry-
Sorry for the really late reply.

This is the code - I pass in the filename I want to save as and this
function takes the PrintDoc object and sends it to the printer as a
file.

I tried just setting the .PrintToFile property to true, but was not
able to get it to print to a file.

In my printing process, I generate the printdoc object's contents
outside this print function as well as the printer settings and printer
name. that stuff is pretty basic. It's this 'reflection' stuff that I
am confused about.


Public Sub PrintToFile(ByVal Filename As String, Optional ByVal
OverwriteFile As Boolean = True)

PrintDoc.PrinterSettings.PrintToFile = True
Dim type As System.Type =
GetType(System.Drawing.Printing.PrinterSettings)
Dim outputPort As System.Reflection.FieldInfo =
type.GetField("outputPort", Reflection.BindingFlags.NonPublic Or
Reflection.BindingFlags.Instance)
outputPort.SetValue(PrintDoc.PrinterSettings, Filename)

Dim printDialogDisplayed As System.Reflection.FieldInfo =
type.GetField("printDialogDisplayed", Reflection.BindingFlags.NonPublic
Or Reflection.BindingFlags.Instance)
printDialogDisplayed.SetValue(PrintDoc.PrinterSettings, False)


'now we have to ensure the path exists
'if not, create it
Dim DirectoryName As String = "C:\"
System.IO.Path.GetDirectoryName(Filename)

If Not System.IO.Directory.Exists(DirectoryName) Then
System.IO.Directory.CreateDirectory(DirectoryName)
End If

If System.IO.File.Exists(Filename) And OverwriteFile Then
System.IO.File.Delete(Filename)
End If

PrintDoc.Print()

End Sub
 
Back
Top