首页 > 其他分享 >已知数据的Type和字符串表示的值反射获取对象

已知数据的Type和字符串表示的值反射获取对象

时间:2023-11-06 18:45:21浏览次数:37  
标签:反射 Convert false string System myType 字符串 Type

转自Convert string to object (c#)_c# string转object-CSDN博客

 

需求:已知数据的Type和string表示的值,据此两个条件生成真正的值,例如Type为System.Boolean,值为false

Type myType = Type.GetType("System.Boolean");
 
var result= Convert.ChangeType("false", myType);

以上仅限基础类型,附上基础类型列表,全类型名待补充

/// <summary>
/// 基础类型列表
/// </summary>
public static List<string> BaseType = new List<string>()
{
    "bool",
    "byte",
    "sbyte",
    "char",
    "short",
    "ushort",
    "int",
    "uint",
    "long",
    "ulong",
    "float",
    "double",
    "decimal",
    "string"
};

 

标签:反射,Convert,false,string,System,myType,字符串,Type
From: https://www.cnblogs.com/MarcLiu/p/17813418.html

相关文章

  • 无涯教程-批处理 - TYPE函数
    此批处理命令将一个或多个文件的内容打印到输出中。TYPE-语法TYPE[filename]其中filename是需要显示其内容的文件。TYPE-示例@echooffTYPEC:\tp\lists.txt文件lists.txt的内容将显示在命令提示符下。参考链接https://www.learnfk.com/batch-script/batch-script......
  • C#解析JSON字符串总结
    JSON文件读取到内存中就是字符串,.NET操作JSON就是生成与解析JSON字符串。操作JSON通常有以下几种方式:1.原始方式:按照JSON字符串自己来解析。 2.通用方式【★★★★★】:这种方式是使用开源的类库Newtonsoft.Json(下载地址http://json.codeplex.com/)。下载后添加dll引用就......
  • Content type 'text/plain;charset=UTF-8' not supported
    Content type 'text/plain;charset=UTF-8' not supported#Content type 'text/plain;charset=UTF-8' not supportedhttps://blog.csdn.net/qwdafedv/article/details/53005418前端TypeError:(0,_login.default)isnotafunction报错#import原因:引......
  • R语言字符串替换
    RgsubFunction gsub()functionreplacesallmatchesofastring,iftheparameterisastringvector,returnsastringvectorofthesamelengthandwiththesameattributes(afterpossiblecoerciontocharacter).Elementsofstringvectorswhichareno......
  • (十一)Python之字符串类型
    字符串类型Python中的字符串用单引号(‘’)或双引号(”“)括起来,同时使用反斜杠(\)转义特殊字符语法:s=”a1a2...an“(n>=0)Python使用单引号(‘)、双引号(“)、三引号(”“”)来表示字符串、其中三引号可以由多行组成,它是编写多行文本的快捷语法,常用于文档字符串,在文件的特定地点,被当作注......
  • 漏扫 X-Content-Type-Options X-XSS-Protection Strict-Transport-Security X-Fram
    web应用nginx部署未设置头部,导致可能出现安全问题【未设置X-Content-Type-Options响应头】【未设置X-XSS-Protection响应头】【未设置Strict-Transport-Security响应头】【X-Frame-Options头未设置】 Content-Type(内容类型),一般是指网页中存在的Content-Type,用于定义网络文......
  • Java去除字符串中空格的方法详解
    1、方法str.trim();str.replace("","");str.replaceAll("","");str.replaceAll("+","");str.replaceAll("\\s*","");\\s*可以匹配空格、制表符、换页符等空白字符的其中任意一个。 2、示例packagetest;publicc......
  • SpringBoot通过自定义注解+反射机制比较两个对象不同的属性值
    publicclassFieldComparisonUtil{/**•直接返回一个新的对象,并且对象的值只有被修改的部分••@paramold•@paramsource•@paramisParent•@paramtarget目标对象•@return/**•@paramold进行属性比较的原始数据•@paramsource进行属性比......
  • 关于yyyy-MM-dd格式日期字符串,解析成LocalDateTime遇到的问题
    LocalDateTimelocalDateTime;try{localDateTime=LocalDateTime.parse(str,DateTimeFormatter.ofPattern(pattern));}catch(Exceptionex){ex.printStackTrace();LocalDatelocalDate=parseLocalDate(str,pattern);......
  • SQL Server中字符串函数LEN 和 DATALENGTH比对
    LEN:返回指定字符串表达式的字符(而不是字节)数,其中不包含尾随空格。DATALENGTH:返回用于表示任何表达式的字节数。示例1:(相同,返回结果都为5): select LEN ('sssss')  select DATALENGTH('sssss')  示例2:(不相同,DATALENGTH是LEN的两倍):  select LEN(N'sssss')  sel......