自动赋值方法CloneObjectData<SourceType, CloneType>-atnet类库

使用场景:在两个有相同属性的类之间拷贝数据,bool ignoreNullValue参数可选是否忽略空值拷贝

方法定义:

/// <summary>
        /// 拷贝对象的数据
        /// </summary>
        /// <param name="s">包含数据的对象</param>
        /// <param name="c">要拷贝数据的对象</param>

        /// <param name="ignoreNullValue">是否忽略空值</param>
       public static void CloneObjectData<SourceType, CloneType>(SourceType s, CloneType c,bool ignoreNullValue);

使用范例:

using Atnet.Data;
   
public static void GetCustomProfile(Account account)
        {
            pa_AccountInfoControl p = new pa_AccountInfoControl { AccountName = account.AccountName };
            p.ExecuteNonQuery();
            DataHelper.CloneObjectData<pa_AccountInfoControl, Account>(p, account, false);
        }

        public static void UpdateCustomProfile(Account account)
        {
            pa_AccountInfoControl p = new pa_AccountInfoControl();
            DataHelper.CloneObjectData<Account,pa_AccountInfoControl>(account, p, false); 
            p.ExecuteNonQuery();
        }

pa_AccountInfoControl为代码生成器生成的存储过程类,Account为一个实体类,方法实现了更新和获得数据!

东方网新,为您提供专业的网站建设,软件开发,网络营销SEO,网络推广服务,代理国外主机空间,域名注册,服务热线:(+86)18608275575,电子邮件:master#atnet.cc;欢迎您访问东方网新网站: www.atnet.cc

源代码:

/*
 * Author   :   Newmin
 * Date     :   2010/10/27
 * siteuri    :   http://blog.atnet.cc/atnetlib/
 */
namespace HuiShi.Data
{
    using System;
    using System.Reflection;
    using System.Collections.Generic;
    using System.Text;

    /// <summary>
    /// 数据辅助类
    /// </summary>
    public static class DataHelper
    {
        /// <summary>
        /// 拷贝对象的数据
        /// </summary>
        /// <param name="s">包含数据的对象</param>
        /// <param name="c">要拷贝数据的对象</param>
        public static void CloneObjectData<SourceType, CloneType>(SourceType s, CloneType c)
        {
            CloneObjectData<SourceType, CloneType>(s, c, true);
        }
        /// <summary>
        /// 拷贝对象的数据
        /// </summary>
        /// <param name="s">包含数据的对象</param>
        /// <param name="c">要拷贝数据的对象</param>
        /// <param name="ignoreNullValue">是否忽略空值</param>
        public static void CloneObjectData<SourceType, CloneType>(SourceType s, CloneType c,bool ignoreNullValue)
        {
            //获取各自的属性
            PropertyInfo[] props = typeof(SourceType).GetProperties();
            PropertyInfo[] cp = typeof(CloneType).GetProperties();

            PropertyInfo _p;                                //PropertyInfo       
            object _pv;                                     //PropertyValue
            foreach (PropertyInfo p in props)
            {
                _p = Array.Find(cp, a => string.Compare(a.Name, p.Name, true) == 0);
                if (_p != null)
                {
                    _pv = p.GetValue(s, null);
                    if (!ignoreNullValue && _pv == null) continue;
                    _p.SetValue(c, _pv, null);
                }
            }
        }
    }
}

 

原创文章转载请注明出处!

原文链接:http://blog.atnet.cc/atnetlib/cloneobjectdata/

atnet项目专栏及源代码下载:http://blog.atnet.cc/atnetlib/ 

 

转载于:https://www.cnblogs.com/newmin/archive/2010/10/28/atnetlib-CloneObjectData.html

<template> <view class="content-cardatail"> <!-- <image class="car-pic" src="/static/images/tabbar/showroom.png" mode=""></image> --> <swiper class="car-pic" :autoplay="true" :interval="3000" :duration="500"> <swiper-item v-for="(item, index) in carImages" :key="index"> <image :src="'https://qicheoss.oss-cn-shanghai.aliyuncs.com'+item" mode="aspectFill" class="swiper-image"></image> </swiper-item> </swiper> <view class="cardatailbox"> <view class="title">{{detailData.carName||''}}</view> <view class="moner"> <text> ¥ <text>{{detailData.estimateSalesPrice||''}}</text> 元 </text> <view class="bg-yugu"> <text class="yugu">预估可赚</text> <text class="money-yugu">{{detailData.estimatedRevenue||''}} 元</text> </view> </view> <view class="title" style="margin-top: 50rpx;">车辆档案</view> <view class="car-message"> <view class="top"> <view class="w-33"> <text>{{detailData.carAge||''}}年内</text> <text class="gray">车辆年限</text> </view> <view class="w-33"> <text>{{detailData.using==1?'营运':'非营运'}}</text> <text class="gray">使用性质</text> </view> <view class="w-33"> <text>{{detailData.mileage||''}}万公里</text> <text class="gray">公里里程</text> </view> </view> <view class="bottom"> <text>车漆颜色: <text class="gray">{{detailData.carProduct.skinColor||''}}</text> </text> <text>内饰颜色: <text class="gray">{{detailData.carProduct.interiorColor||''}}</text> </text> </view> </view> <!-- 车辆信息 --> <view class="content-money"> <view class="flex" style="margin-top: 0px;"> <text>维修费</text> <text class="gray">{{detailData.repairFee||''}}元</text> </view> <view class="flex"> <text>关税</text> <text class="gray">{{detailData.tariff||''}}元</text> </view> <view class="flex"> <text>报关费用</text> <text class="gray">{{detailData.customsFee||''}}元</text> </view> <view class="flex"> <text>清关费用</text> <text class="gray">{{detailData.clearanceFee||''}}元</text> </view> <view class="flex"> <text>运输费用</text> <text class="gray">{{detailData.transportFee||''}}元</text> </view> <view class="flex"> <text>合计采购价</text> <text class="gray">{{detailData.allMoney||''}}元</text> </view> </view> <view class="title" style="margin-top: 30rpx;">采购基本信息</view> <u--form labelPosition="left" :model="formData" :rules="rules" ref="uForm"> <view class="bg_color"> <u-form-item label="采购日期" :labelWidth="100" prop="purchaseTime" borderBottom ref="item1" > <!-- <view v-if="showDatePicker"> --> <dataTimeVue @confirm="confirmDate" @cancel="showDatePicker = false" title="选择采购时间" mode="date" v-model="formData.purchaseTime" ></dataTimeVue> <!-- </view> --> <view class="pickerbox" @click="showDatePicker=true"> </view> </u-form-item> <u-form-item label="采购人" :labelWidth="100" prop="purchase" borderBottom ref="item1" > <u--input v-model="formData.purchase" placeholder='请输入' :error-message="errorMessage.purchase" border="none"> </u--input> </u-form-item> <u-form-item label="采购价格" :labelWidth="100" prop="purchasePrice" borderBottom ref="item1" > <u--input v-model="formData.purchasePrice" placeholder='请输入' type="number" :error-message="errorMessage.purchasePrice" border="none"> </u--input> </u-form-item> <u-form-item label="车架号" :labelWidth="100" prop="carVin" borderBottom ref="item1" > <u--input v-model="formData.carVin" placeholder='请输入' :error-message="errorMessage.carVin" border="none"> </u--input> </u-form-item> </view> <view class="content_foot"> <label>支付截图</label> <view class="upload-box"> <view class="content flex-wrap align-center"> <view class="image-box rel" v-for="(item,index) in imageList" :key="index"> <!-- #ifdef MP-WEIXIN --> <image :src="item" mode=""></image> <image @click="DelPic(index)" class="reduce abs" :src="aliyunOssUrl+'/static/images/ygcmp/home/car1.png'" mode=""></image> <!-- #endif --> <!-- #ifdef APP --> <image :src="item" mode=""></image> <image @click="DelPic(index)" class="reduce abs" :src="aliyunOssUrl+'/static/images/ygcmp/home/car2.png'" mode=""></image> <!-- #endif --> </view> <!-- #ifdef MP-WEIXIN --> <image :src="aliyunOssUrl+'/static/images/ygcmp/home/changchuan.png'" mode="" @click="getImage"></image> <!-- #endif --> <!-- #ifdef APP --> <image :src="aliyunOssUrl+'/static/images/ygcmp/home/changchuan.png'" mode="" @click="getImage"></image> <!-- #endif --> </view> </view> </view> <view class="content_foot"> <label>车辆证件</label> <view class="upload-box"> <view class="content flex-wrap align-center"> <view class="image-box rel" v-for="(item,index) in imageList" :key="index"> <!-- #ifdef MP-WEIXIN --> <image :src="item" mode=""></image> <image @click="DelPic(index)" class="reduce abs" :src="aliyunOssUrl+'/static/images/ygcmp/home/car1.png'" mode=""></image> <!-- #endif --> <!-- #ifdef APP --> <image :src="item" mode=""></image> <image @click="DelPic(index)" class="reduce abs" :src="aliyunOssUrl+'/static/images/ygcmp/home/car2.png'" mode=""></image> <!-- #endif --> </view> <!-- #ifdef MP-WEIXIN --> <image :src="aliyunOssUrl+'/static/images/ygcmp/home/changchuan.png'" mode="" @click="getImage"></image> <!-- #endif --> <!-- #ifdef APP --> <image :src="aliyunOssUrl+'/static/images/ygcmp/home/changchuan.png'" mode="" @click="getImage"></image> <!-- #endif --> </view> </view> </view> </u--form> <!-- 提交按钮 --> <view class="fixed-submit-btn"> <button class="submit-btn" @click="handleSubmit" :style="{background: 'linear-gradient(97deg, #4088F7 0%, #3759EF 100%)'}" > 提交采购信息 </button> </view> </view> </view> </template> <script> import { split } from 'lodash'; import dataTimeVue from './uni-datetime-picker/components/uni-datetime-picker/uni-datetime-picker.vue' import { getDetailCar } from '@/api/activity/activity.js' export default { data() { return { orderid:'', detailData:{ }, showDatePicker:false, imageList: [], errorMessage: { purchaseTime: '',//采购时间 purchase:'',//采购人 purchasePrice:'',//采购价格 }, rules: { purchaseTime: [{ required: true, message: '请选择采购时间', trigger: ['blur', 'change'] }], purchase: [{ required: true, message: '请输入采购人', trigger: ['blur', 'change'] }], purchasePrice: [{ required: true, message: '请输入采购价格', trigger: ['blur', 'change'] }], carVin: [{ required: true, message: '请输入车架号', trigger: ['blur', 'change'] }] }, aliyunOssUrl:'https://cbl.diyouzhijia.com', carImages:[], formData:{ purchaseTime: '',//采购时间 purchase:'',//采购人 purchasePrice:'',//采购价格 carVin:'',//车架号 }, }; }, components: { dataTimeVue }, onReady(){ this.$refs.uForm.setRules(this.rules) }, onLoad(options) { this.orderid=options.id this.getDeatail() }, methods: { // 调用详情页接口数据 getDeatail(){ getDetailCar({ orderId:this.orderid }).then(res=>{ console.log("详情页接口数据",res); this.detailData=res.data this.carImages=res.data.banner.split(",") this.detailData.allMoney=Number(this.detailData.purchasePrice)+Number(this.detailData.repairFee)+Number(this.detailData.tariff)+Number(this.detailData.customsFee)+Number(this.detailData.clearanceFee)+Number(this.detailData.transportFee) }) }, confirmDate(){ console.log(111,"111"); }, handleSubmit() { console.log(this.formData,"handleSubmit"); return this.$refs.uForm.validate().then(valid => { if (valid) { uni.showLoading({ title: '提交中...' }); setTimeout(() => { uni.hideLoading(); uni.showToast({ title: '提交成功', icon: 'success' }); // 提交成功后可以跳转或其他操作 // uni.navigateBack(); }, 1500); } else { uni.showToast({ title: '请填写完整信息', icon: 'none' }); } }); }, // 删除图片 DelPic(index) { this.imageList.splice(index, 1) }, //传图片 getImage() { // uni.showLoading({ // title: '上传中' // }) this.$util.uploadImageOne({ count: 9, sizeType: ['original'], sourceType: ['album', 'camera'], dir: 'lxkimage/public/product/' }, null, res => { this.imageList.push(res.data) // uni.hideLoading(); },err=>{ // uni.hideLoading(); }); }, // 预览图片单张 previewImage(num) { let imgsArray = this.itemlist; uni.previewImage({ current: num, urls: imgsArray, }); }, } }; </script> <style lang="scss" scoped> .content-cardatail{ padding-bottom: 200rpx; .car-pic{ width: 100%; height: 520rpx; } .cardatailbox{ width: 100%; padding: 30rpx; position: relative; top: -42rpx; border-radius: 20px 20px 0px 0px; background:white url('https://cbl.diyouzhijia.com/static/images/ygctzmp/home/xigua.png') no-repeat; background-size: 100% 204rpx; .title{ color: #333333; font-size: 32rpx; font-weight: 500; } .moner{ display: flex; align-items: center; margin-top: 22rpx; text{ color: #FF6400; font-size: 24rpx; text{ font-size: 42rpx; font-weight: bold; padding-right: 3rpx; } } .bg-yugu{ margin-left: 50rpx; width: 325rpx; height: 48rpx; font-size: 24rpx; font-weight: 500; color: white; line-height: 44rpx; // border: 1px solid #3E80F5; background: url('https://cbl.diyouzhijia.com/static/images/ygcmp/home/yugu.png') no-repeat; background-size: 100% 100%; display: flex; align-items: center; justify-content: space-between; padding: 8rpx 8rpx 9rpx 14rpx; .yugu{ font-size: 24rpx; color: white; font-weight: bold; } .money-yugu{ color: #2F74FA; font-size: 32rpx; font-weight: bold; } } } .car-message{ height: 220rpx; padding: 30rpx; font-size: 28rpx; background: #F9FBFF; border-radius: 20rpx; color: #333333; margin-top: 20rpx; padding-left: 0px; padding-right: 0px; .gray{ color: #666666; padding-top: 20rpx; } .top{ width: 100%; height: 71%; display: flex; align-items: center; justify-content: space-between; border-bottom: 1px dashed #B3B3B3; .w-33{ width: 33%; height: 100%; display: flex; flex-direction: column; align-items: center; // justify-content: center; } } .bottom{ display: flex; width: 100%; height: 40%; justify-content: space-between; align-items: center; padding: 0 30rpx; } } .content-money{ width: 100%; height: 472rpx; margin-top: 30rpx; background: #F9FBFF; border-radius: 20px; padding: 40rpx 30rpx; .flex{ display: flex; margin-top: 32rpx; align-items: center; color: #333333; font-size: 28rpx; justify-content: space-between; } } // 信息截止 } } </style> <style lang="scss" scoped> // 表单样式以及穿透可复制 /deep/ .u-form-item__body{ display: flex; input{ text-align: right !important; } } /deep/.u-form-item__body__right__message{ text-align: right !important; } .bg_color{ padding: 30rpx; border-radius: 20rpx; margin-top: 20rpx; background: rgba(200, 225, 255, 0.1); } /deep/.uni-date{ .uni-date-editor--x{ border: 0px solid red !important; .uni-date-x{ background: none !important; padding: 0px !important; input{ padding: 0px; } uni-icons{ display: none !important; } } } } .content_foot { margin-top: 30rpx; font-size: 30rpx; padding: 0 24rpx; font-weight: bold; color: #111111; margin-bottom: 50rpx; .upload-box { margin-top: 16rpx; } .content { padding: 32rpx 0 64rpx 0; .image-box { position: relative; margin-right: 24rpx; margin-bottom: 24rpx; width: 100rpx; height: 100rpx; .reduce { position: absolute; top: -16rpx; right: -16rpx; width: 32rpx; height: 32rpx; } } image { width: 100rpx; height: 100rpx; border-radius: 8rpx; } } } // 在最后一个 <style> 块中添加 .fixed-submit-btn { position: fixed; bottom: 0; left: 0; right: 0; z-index:1; padding: 20rpx 30rpx; background: white; box-shadow: 0 -4rpx 12rpx rgba(0, 0, 0, 0.05); .submit-btn { width: 100%; height: 88rpx; line-height: 88rpx; border-radius: 44rpx; color: white; font-size: 32rpx; font-weight: bold; border: none; &::after { border: none; // 去除按钮默认边框 } &:active { opacity: 0.9; } } } // 确保表单内容不会被按钮遮挡 .cardatailbox { padding-bottom: 120rpx; // 给底部按钮留出空间 } </style> <style lang="scss"> .pickerbox{ .u-input{ background-color: rgba(200, 225, 255,0) !important; } } </style> 车架号只能输入大小写字母加数字
07-31
<template> <view class="scan-container"> <!-- 相机视图 --> <camera v-if="cameraActive && hasPermission" class="camera" device-position="back" flash="off" @error="cameraError"></camera> <!-- 操作按钮区域 --> <view class="controls"> <!-- <button class="btn" @click="chooseImage">从相册选择</button> --> </view> <!-- 结果弹窗 --> <uni-popup ref="resultPopup" type="dialog"> <uni-popup-dialog title="扫码结果" :content="scanResult" confirmText="复制" @confirm="copyResult"></uni-popup-dialog> </uni-popup> </view> </template> <script setup> import { ref, onMounted, onUnmounted } from 'vue' import { onLoad } from '@dcloudio/uni-app' // 状态管理 const cameraActive = ref(false) const scanResult = ref('') const resultPopup = ref(null) onLoad((e) => { initScan() }) onUnmounted(() => { stopScan() }) // 初始化扫码 const initScan = async () => { cameraActive.value = true setTimeout(scanQRCode, 300) // 稍等相机初始化 } // 停止扫描 const stopScan = () => { cameraActive.value = false } // 扫码功能 const scanQRCode = () => { uni.scanCode({ onlyFromCamera: true, // 只允许相机扫码 scanType: ['qrCode'], success: (res) => { handleScanResult(res.result) }, fail: (err) => { console.error('扫码失败:', err) uni.showToast({ title: '扫码失败', icon: 'none' }) }, complete: () => { cameraActive.value = false } }) } // 从相册选择图片 const chooseImage = async () => { try { const res = await uni.chooseImage({ count: 1, sourceType: ['album'] }) uni.scanCode({ path: res.tempFilePaths[0], success: (scanRes) => { handleScanResult(scanRes.result) }, fail: () => { uni.showToast({ title: '未识别到二维码', icon: 'none' }) } }) } catch (err) { console.error('图片选择失败:', err) } } // 处理扫码结果 const handleScanResult = (result) => { scanResult.value = result cameraActive.value = false resultPopup.value.open() } // 显示结果弹窗 const showResultModal = () => { uni.showModal({ title: '扫码结果', content: scanResult.value, confirmText: '复制', success: (res) => { if (res.confirm) { uni.setClipboardData({ data: scanResult.value }) } } }) } // 相机错误处理 const cameraError = (e) => { console.error('相机错误:', e.detail) uni.showToast({ title: '相机初始化失败', icon: 'none' }) cameraActive.value = false } </script> <style scoped> .scan-container { display: flex; flex-direction: column; height: 100vh; } .camera { flex: 1; width: 100%; } .controls { padding: 20rpx; display: flex; justify-content: space-around; background: #f5f5f5; } .btn { width: 45%; background-color: #07C160; color: white; border-radius: 10rpx; } </style>这段代码中想要加入可以扫描相册中的二维码,选择图片后直接扫描
06-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值