zoukankan      html  css  js  c++  java
  • 反射的应用

    Imports System.Reflection
    Module Module1
    Sub Main()
    Dim p As New Person
    p.Name
    = "cuishengli"
    p.Age
    = 30
    p.Tall
    = 1.78
    p.Birth
    = "1978-09-18"

    Console.WriteLine(
    "dict:")
    Dim dict = GetInformation(p)
    For Each i In dict
    Console.WriteLine(
    "key={0};Value={1}", i.Key, i.Value)
    Next

    Console.WriteLine(
    "P2:")
    Dim p2 = Create(Of Person)(dict)
    Console.WriteLine(p2.ToString)

    Console.ReadLine()
    End Sub

    Function Create(Of T As New)(dict As Dictionary(Of String, String)) As T
    Dim obj As New T
    Dim tp As Type = GetType(T)
    For Each pi In tp.GetProperties
    Select Case pi.PropertyType.Name
    Case "String"
    pi.SetValue(obj, dict(pi.Name),
    Nothing)
    Case "Double"
    pi.SetValue(obj,
    CDbl(dict(pi.Name)), Nothing)
    Case "Int32"
    pi.SetValue(obj,
    CInt(dict(pi.Name)), Nothing)
    Case "DateTime"
    pi.SetValue(obj,
    Date.Parse(dict(pi.Name)), Nothing)
    Case Else
    Throw New Exception("create 不能处理类型:" & pi.PropertyType.Name)
    End Select
    Next
    Return obj
    End Function

    Function GetInformation(obj As Object) As Dictionary(Of String, String)
    Dim dict As New Dictionary(Of String, String)
    Dim tp = obj.GetType
    For Each pi In tp.GetProperties
    dict.Add(pi.Name, pi.GetValue(obj,
    Nothing))
    Next
    Return dict
    End Function

    End Module
  • 相关阅读:
    ES6新语法之---块作用域let/const(2)
    sass变量
    Ruby(或cmd中)输入命令行编译sass
    sass的安装
    鼠标滚动兼容
    HTML5新标签兼容——> <!--<if lt IE 9><!endif-->
    #include stdio.h(7)
    #include stdio.h(6)
    #include stdio.h(5)
    #include stdio.h(3)
  • 原文地址:https://www.cnblogs.com/cuishengli/p/1999143.html
Copyright © 2011-2022 走看看