ColdFusion 9.0 Resources |
cfobject: .NET objectDescriptionCreates a .NET object, that is, a ColdFusion proxy for accessing a class in a local or remote .NET assembly. Syntax<cfobject class="class name" name="instance name" type=".NET|dotnet" action="create" assembly="absolute path" port="6086" protocol="tcp|http" secure="no|yes" server = "localhost"> Note: You can
specify this tag’s attributes in an attributeCollection attribute
whose value is a structure. Specify the structure name in the attributeCollection attribute
and use the tag’s attribute names as structure keys.
See alsoCreateObject: .NET object, DotNetToCFType, Using Microsoft .NET Assemblies in the Developing ColdFusion Applications HistoryColdFusion 8: Added .NET and dotnet type values, and the assembly, port, protocol, and secure attributes. Attributes
UsageThe cfobject tag with a .NET or dotnet value for the type attribute creates a reference to a .NET object of a given class. Using the reference, you can access the .NET object’s fields and methods. The .NET classes do not have to be local, and you can use the cfobject tag on a system that does not have .NET installed, including UNIX-based or OS-X systems. To access .NET assemblies, do the following:
Accessing methods and fields You call .NET methods as you use any other ColdFusion object methods. In the simplest case, your application code uses the following format to call a .NET class method: <cfobject type=".NET" name="mathInstance" class="mathClass"> assembly="C:/Net/Assemblies/math.dll"> <cfset myVar=mathInstance.multiply(1,2)> If a .NET class has multiple constructors, and you do not want ColdFusion to use the default constructor to create the object, invoke a specific constructor by calling the special init method of the ColdFusion object with the constructor’s arguments. For example, you can use the following tags to instantiate com.foo.MyClass(int, int): <cfobject type=".NET" class="com.foo.MyClass" assembly="c:\temp\myLib.dll" name="myObj" > <cfset myObj.init(10, 5)> You access and change .NET class public fields by calling the following methods: Get_fieldName() Set_fieldName() For example, if the .NET class has a public field named account, you can access and modify its value by using Get_acount() and Set_account() methods, respectively. You can access, but not modify final fields, so you can only call Get_fieldName() for these fields. ExampleThe following example uses the GetProcess method of the .NET System.Diagnostics.Process class to get and display information about the processes running on the local system. Because it uses a core .NET class, for which ColdFusion automatically generates proxies, you do not have to specify an assembly name in the cfobject tag. For more complex examples, including examples that use custom .NET classes, see Using Microsoft .NET Assemblies in the Developing ColdFusion Applications. <cfobject type=".NET" name="proc" class="System.Diagnostics.Process"> <cfset processes = proc.GetProcesses()> <cfset arrLen = arrayLen(processes)> <table border=0 cellspacing="3" cellpadding="3"> <tr bgcolor="#33CCCC"> <td style="font-size:12px; font-weight:bold" nowrap>Process ID</td> <td style="font-size:12px; font-weight:bold" nowrap>Name</td> <td style="font-size:12px; font-weight:bold" nowrap>Memory (KB)</td> <td style="font-size:12px; font-weight:bold" nowrap>Peak Memory (KB)</td> <td style="font-size:12px; font-weight:bold" nowrap>Virtual Memory Size (KB)</td> <td style="font-size:12px; font-weight:bold" nowrap>Start Time</td> <td style="font-size:12px; font-weight:bold" nowrap>Total Processor Time</td> </tr> <cfloop from = 1 to="#arrLen#" index=i> <cfset process = processes[i]> <cfset id = process.Get_Id()> <cfif id neq 0> <cfoutput> <tr> <td align="right">#process.Get_Id()#</td> <td>#process.Get_ProcessName()#</td> <td align="right">#process.Get_PagedMemorySize()/1000#</td> <td align="right">#process.Get_PeakPagedMemorySize()/1000#</td> <td align="right">#process.Get_VirtualMemorySize()/1000#</td> <td>#process.Get_StartTime()#</td> <td>#process.Get_TotalProcessorTime()#</td> </tr> </cfoutput> </cfif> </cfloop> </table> |