1.tryparse Դ?源码?
2.C#操作TXT文本的源代码!
3.详解.NET Model ValidationAttributeç使ç¨
tryparse Դ?源码java怎样导入源码?
class Program
{
static void Main(string[] args)
{
//累加值
double totalValue = 0;
//输入个数字
for (int i = 0; i < ; i++)
{
Console.Write("请输入数组arr中的第{ 0}个元素的值:", i);
//接收输入的数字
var input = Console.ReadLine();
int number;
var result = int.TryParse(input, out number);
if (!result)
{
i--;
}
else
{
totalValue += number;
}
}
//计算平均值
var avg = totalValue / ;
Console.WriteLine("数组array的平均值为:{ 0}", avg);
Console.ReadLine();
}
}
笑纳~
C#操作TXT文本的源代码!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace OutputText
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Load += new EventHandler(Form1_Load);
}
void Form1_Load(object sender,源码0.11011源码转反码 EventArgs e)
{
this.btnShow.Click += new EventHandler(btnShow_Click);
}
void btnShow_Click(object sender, EventArgs e)
{
// 判断输入的行数
string input = this.txtInput.Text.Trim();
//用于读文本的时候记数
int numOfTextFile;
// int.TryParse验证输入的是不是行数
if (int.TryParse(input, out numOfTextFile))
{
//StreamReader 打开文件
using(StreamReader sr = new StreamReader(@"D:\text.txt"))
{
int count=0;
// ReadLine 读一行
string txtContent = sr.ReadLine();
while (txtContent != null)
{
//读一行记一下数
count++;
//如果和你要求的输入行数匹配,显示文本,源码把补码变成源码中断循环
if (count == numOfTextFile)
{
this.textBox1.Text = txtContent;
this.textBox2.Text = txtContent;
this.textBox3.Text = sr.ReadLine();
break;
}
else
{
txtContent = sr.ReadLine();
}
}
}
}
}
}
}
详解.NET Model ValidationAttributeç使ç¨
ä¸ãç®ä»
ValidationAttribute éªè¯ç¹æ§ä¸è¬ç¨æ¥éªè¯æ°æ®çæ ¼å¼ï¼èå´ï¼æ¯å¦å¿ å¡«çï¼æ们éè¿å®çåç±»ç¹æ§ RangeãRequired çç¹æ§å¯ä»¥è½»æ¾å®ç°å¯¹æ°æ®çéªè¯ãä½æ¯å¯¹äºä¸äºç¹æ®éè¦çç¹æ§ï¼ç³»ç»èªå¸¦çç¹æ§å±éæ§å¾å¤§ï¼æ们ä¹å¯ä»¥èªå®ä¹æ©å±éè¦çç¹æ§ã ?源码pco挖矿源码
äºãASP.NET åºç¨ValidationAttribute åºç¨å¨ ASP.NET MVC ä¸æ¯å¸¸ç¨å¾ï¼ç¨äºå¯¹ HTPP Request åæ°å段å¾æ ¡éªï¼ä¸éè¿å¾ä¼è¿å æ示ç»è°ç¨æ¹ãæ¥ä¸éè¿ä¸ä¸ªç®åä¾åè¿è¡è¯´æä¸ä¸ï¼ä¾å使ç¨å¾æ¯ .NET Web API ã
2.1 Modelé¦å å®ä¹ä¸ä¸ª request model æ·»å ä¸äºå段ï¼å¦ Name æä¸ RequiredãMinLengthãMaxLength è¿æ ·çæ è®°è¿è¡éå¶
public?class?PersonalRequest{ public?int?Id?{ ?get;?set;?}[Required][MinLength(2),?MaxLength()]public?string?Name?{ ?get;?set;?}public?string?Sex?{ ?get;?set;?}[Required][MinLength(2),?MaxLength()]public?string?Address?{ ?get;?set;?}}2.2 Controllerå®ä¹å® request ?model åï¼å建ä¸ä¸ª ?API ï¼å°ä¸ä¸æ¥å®ä¹ç request model ä½ä¸ºåæ°ãæ¤æ¶å¨ request ?model å段ä¸æç ?RequiredãMinLengthãMaxLength æ è®°ï¼æ¯æ²¡æä»»ä½ä½ç¨çã
[HttpPost][Route("WebAPIApply")]public?IActionResult?WebAPIApply(PersonalRequest?request){ return?Ok();}2.4 Filter为äºè®© request ?model å段ä¸æç ?RequiredãMinLengthãMaxLength æ è®°çæï¼ MVC ä¸éè¦ä½¿ç¨å° Filter ã代ç å¦ä¸
public?class?ModelValidateFilter?:?IAsyncActionFilter{ public?async?Task?OnActionExecutionAsync(ActionExecutingContext?context,?ActionExecutionDelegate?next){ if?(!context.ModelState.IsValid){ var?allErrors?=?context.ModelState.Values.SelectMany(v?=>?v.Errors);var?message?=?string.Join("?|?",?allErrors.Select(e?=>?e.ErrorMessage));context.Result?=?new?JsonResult(new?{ ?results?=?allErrors?});}await?next();}}2.5 注åFilter å建å®æåï¼éè¦æ³¨åä¸ä¸ï¼è¿éæ们使ç¨å ¨å±æ¨¡å¼ãå¦ä¸
builder.Services.AddMvc(opt?=>{ opt.Filters.Add(new?ModelValidateFilter());});2.6 éªè¯ç¨åºå建å®æåï¼è°ç¨ API éªè¯ä¸ä¸ï¼å¦ä¸å¯¹ Name åå«èµå¼äº D Daã å¯ä»¥çå°å½æ¯ D çæ¶åï¼å段é¿åº¦ä¸º 1 ï¼filter 对 request ?model è¿è¡äºæ£ç¡®çæ ¡éª
ä¸ãç´æ¥éªè¯å®ä¾ç´æ¥éªè¯å®ä¾ï¼æ¯æå¯ä»¥å¯¹å®ä¹ model çå®ä¾ï¼èªå®ä¹ä»£ç 对é½è¿è¡æ ¡éªï¼è¿ç§ä½¿ç¨èµ·æ¥ç¸å¯¹çµæ´»ï¼ä½ä¹ä¸ç»å¸¸ä½¿ç¨ãæ¥ä¸éè¿ä¸ä¸ªç®åä¾åè¿è¡è¯´æä¸ä¸
3.1 Modelé¦å å®ä¹ä¸ä¸ª model æ·»å ä¸äºå段ï¼å¦ Name æä¸ RequiredãMinLengthãMaxLength è¿æ ·çæ è®°è¿è¡éå¶
public?class?Personal{ [Required]public?int?ID?{ ?get;?set;?}[Required][MinLength(2),?MaxLength(5)]public?string?Name?{ ?get;?set;?}}3.2 Validation Codeæ ¡éªä»£ç å¦ä¸ï¼å 对å®ä¹ç model è¿è¡äºå®ä¾åï¼ç¶åä½¿ç¨ Validator.TryValidateObject æ¹æ³è¿è¡æ ¡éªï¼å®é å·¥ä½ä¸éè¦èªå·±è¿è¡ä¸äºå°è£ ãå ³é®ä»£ç å¦ä¸ï¼
Personal?personal?=?new?Personal();//?personal.Name?=?"D";ValidationContext?validationContext?=?new?ValidationContext(personal);List<ValidationResult>?results?=?new?List<ValidationResult>();bool?isValid?=?Validator.TryValidateObject(personal,?validationContext,?results,?true);3.3 éªè¯ä»£ç åå®åï¼è°ç¨ API éªè¯ä¸ä¸ãé¦å ç´æ¥è°ç¨ï¼åºä¸ºä»£ç éé¢æ²¡æ对 Personal å®ä¾èµå¼ï¼æ£ç¡®çè¿è¡äº Required æ è®°çæ ¡éªå¯ä»¥ä¿®æ¹ä¸ä»£ç ?personal.Name = "Da"ï¼ç¶åå¯å¨ç¨åºï¼è°ç¨ä¸ä¸ API å¯ä»¥çå°æ ¡éªæ¯éè¿çå¯ä»¥ä¿®æ¹ä¸ä»£ç ?personal.Name = "D"ï¼ç¶åå¯å¨ç¨åºï¼è°ç¨ä¸ä¸ API å¯ä»¥çå°æ ¡éªæ¯ä¸éè¿çï¼å 为é¿åº¦ä¸º 1 没æ满足 MinLength(2)
åãèªå®ä¹ ValidationAttributeé¤äºå¯ä»¥ä½¿ç¨ Microsoft æä¾ç ValidationAttribute ï¼æ们è¿å¯ä»¥ä½¿ç¨èªå®ä¹ç ValidationAttribute ï¼å¾®è½¯è¿æ¹é¢æä¾äºå¯æ©å±æ§ãèªå®ä¹ç ValidationAttribute åªéç»§æ¿ ValidationAttributeï¼éå IsValid ãFormatErrorMessage å³å¯ã使ç¨æ¹é¢ä¸ Microsoft æä¾ç ValidationAttribute 使ç¨ä¸æ¨¡ä¸æ ·ãå¦ä¸ä»£ç
public?class?CanToIntAttribute?:?ValidationAttribute{ ///?<summary>///?IsValid?为?false?æ¶ï¼æ示å¾?error?ä¿¡æ¯///?</summary>///?<param?name="name"></param>///?<returns></returns>public?override?string?FormatErrorMessage(string?name){ return?$"{ name}?need?to?int";}///?<summary>///?éªè¯å½åå段å¾ç»æ///?</summary>///?<param?name="value"></param>///?<returns></returns>public?override?bool?IsValid(object?value){ int?num?=?0;return?int.TryParse(Convert.ToString(value),?out?num);}}public?class?PersonalRequest{ public?int?Id?{ ?get;?set;?}public?string?Name?{ ?get;?set;?}[CanToInt]public?string?Sex?{ ?get;?set;?}[Required][MinLength(2),?MaxLength()]public?string?Address?{ ?get;?set;?}}äºãæºç /post/