微软.net精简框架常见问题及回答(中文版)(68)

王朝c#·作者佚名  2006-01-09
宽屏版  字体: |||超大  

for (int i = 0; i < propInfo.Length; i++)

{

if (propInfo[i].Name == "Width")

{

width = (int)propInfo[i].GetValue(bm, null);

break;

}

}

// Call the GetPixel method

MethodInfo[] methInfo = bm.GetType().GetMethods(BindingFlags.Public | BindingFlags.Instance);

for (int i = 0; i < methInfo.Length; i++)

{

if (methInfo[i].Name == "GetPixel")

{

ParameterInfo[] paramInfo = methInfo[i].GetParameters();

if (paramInfo.Length == 2)

{

object[] xy = new object[2];

if (paramInfo[0].Name == "x")

{

xy[0] = x;

xy[1] = y;

}

else

{

xy[1] = x;

xy[0] = y;

}

pixColor = (Color)methInfo[i].Invoke(bm, xy);

break;

}

'VB

Imports System.Reflection

Imports System.Drawing

Dim bm As New Bitmap(200, 100)

Dim width As Integer = 0

' Explicitly set one pixel for testing

Dim x As Integer = 199

Dim y As Integer = 20

Dim pixColor As Color = Color.Black

bm.SetPixel(x, y, Color.Magenta)

' Get the "Width" property

Dim propInfo As PropertyInfo() = _

bm.GetType().GetProperties((BindingFlags.GetProperty Or _

BindingFlags.Public Or BindingFlags.Instance))

Dim i As Integer

For i = 0 To propInfo.Length - 1

If propInfo(i).Name = "Width" Then

width = Fix(propInfo(i).GetValue(bm, Nothing))

Exit For

End If

Next i

' Call the SetPixel method

Dim methInfo As MethodInfo() = bm.GetType().GetMethods((BindingFlags.Public _

Or BindingFlags.Instance))

For i = 0 To methInfo.Length - 1

If methInfo(i).Name = "GetPixel" Then

Dim paramInfo As ParameterInfo() = methInfo(i).GetParameters()

If paramInfo.Length = 2 Then

Dim xy(1) As Object

If paramInfo(0).Name = "x" Then

xy(0) = x

xy(1) = y

Else

xy(1) = x

xy(0) = y

End If

pixColor = CType(methInfo(i).Invoke(bm, xy), Color)

Exit For

End If

End If

Next i

7.30. How do I determine the device name programatically?

The device name can be accessed through the System.Net namespace, as demonstrated by the following code. //C#

String devName = System.Net.Dns.GetHostName();

'VB

Dim devName As String = System.Net.Dns.GetHostName()

第一页    上一页    第68页/共78页    下一页    最后页
第01页 第02页 第03页 第04页 第05页 第06页 第07页 第08页 第09页 第10页 
第11页 第12页 第13页 第14页 第15页 第16页 第17页 第18页 第19页 第20页 
第21页 第22页 第23页 第24页 第25页 第26页 第27页 第28页 第29页 第30页 
第31页 第32页 第33页 第34页 第35页 第36页 第37页 第38页 第39页 第40页 
第41页 第42页 第43页 第44页 第45页 第46页 第47页 第48页 第49页 第50页 
第51页 第52页 第53页 第54页 第55页 第56页 第57页 第58页 第59页 第60页 
第61页 第62页 第63页 第64页 第65页 第66页 第67页 第68页 第69页 第70页 
第71页 第72页 第73页 第74页 第75页 第76页 第77页 第78页 
 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有