如何使用C#中的反射将对象转换为其他类型的对象?

时间:2013-01-01 13:51:18

标签: c# asp.net asp.net-mvc c#-4.0 c#-3.0

我有两个类,它们也包含集合类型属性。我创建了一个第一类对象,然后将其转换为另一个类对象。我无法将其转换为其他类型的对象。

我的头等舱: -

 public partial class Product
{
    public Product()
    {
        this.Product_Category_Mapping = new HashSet<Product_Category_Mapping>();
        this.Product_Manufacturer_Mapping = new HashSet<Product_Manufacturer_Mapping>();
        this.Product_Picture_Mapping = new HashSet<Product_Picture_Mapping>();
        this.ProductReviews = new HashSet<ProductReview>();
        this.Product_SpecificationAttribute_Mapping = new HashSet<Product_SpecificationAttribute_Mapping>();
        this.ProductVariants = new HashSet<ProductVariant>();
        this.ProductTags = new HashSet<ProductTag>();
    }

    public int Id { get; set; }
    public string Name { get; set; }
    public string ShortDescription { get; set; }
    public string FullDescription { get; set; }
    public string AdminComment { get; set; }
    public int ProductTemplateId { get; set; }
    public bool ShowOnHomePage { get; set; }
    public string MetaKeywords { get; set; }
    public string MetaDescription { get; set; }
    public string MetaTitle { get; set; }
    public bool AllowCustomerReviews { get; set; }
    public Nullable<decimal> ApprovedRatingSum { get; set; }
    public Nullable<decimal> NotApprovedRatingSum { get; set; }
    public int ApprovedTotalReviews { get; set; }
    public int NotApprovedTotalReviews { get; set; }
    public bool Published { get; set; }
    public bool Deleted { get; set; }
    public System.DateTime CreatedOnUtc { get; set; }
    public System.DateTime UpdatedOnUtc { get; set; }
    public string FactSheet { get; set; }
    public string VisitorTestimonials { get; set; }
    public string areasofinterest { get; set; }
    public string inventory { get; set; }
    public string GeneralInfo { get; set; }
    public string HowToReach { get; set; }
    public string ShortComment { get; set; }
    public Nullable<int> CountryId { get; set; }
    public Nullable<int> StateProvinceId { get; set; }
    public string City { get; set; }
    public string LandMark { get; set; }
    public Nullable<int> CityId { get; set; }
    public Nullable<int> LandMarkId { get; set; }
    public int MealType { get; set; }
    public Nullable<bool> IsVerified { get; set; }
    public Nullable<bool> IsPrivate { get; set; }
    public Nullable<bool> IsEnabled { get; set; }
    public Nullable<bool> IsMoneyBack { get; set; }
    public string ItineraryOption1 { get; set; }
    public string ItineraryOption2 { get; set; }
    public string ItineraryOption3 { get; set; }
    public string ItineraryOption4 { get; set; }
    public string ItineraryOption5 { get; set; }
    public string ItineraryOption6 { get; set; }
    public string ItineraryOption7 { get; set; }
    public string ItineraryOption8 { get; set; }
    public string ItineraryOption9 { get; set; }
    public string ItineraryOption10 { get; set; }
    public Nullable<int> VendorId { get; set; }
    public string PromotionalMessage { get; set; }
    public Nullable<int> PurchaseRewardPoint { get; set; }
    public Nullable<int> MaximumConsumeRewardPoint { get; set; }
    public Nullable<int> MinimumRewardPoint { get; set; }
    public Nullable<bool> IsTrip { get; set; }
    public Nullable<int> TripType { get; set; }
    public Nullable<int> MinimumBookingforTrip { get; set; }
    public Nullable<bool> IsGuranted { get; set; }

    public virtual ICollection<Product_Category_Mapping> Product_Category_Mapping { get; set; }
    public virtual ICollection<Product_Manufacturer_Mapping> Product_Manufacturer_Mapping { get; set; }
    public virtual ICollection<Product_Picture_Mapping> Product_Picture_Mapping { get; set; }
    public virtual ICollection<ProductReview> ProductReviews { get; set; }
    public virtual ICollection<Product_SpecificationAttribute_Mapping> Product_SpecificationAttribute_Mapping { get; set; }
    public virtual ICollection<ProductVariant> ProductVariants { get; set; }
    public virtual ICollection<ProductTag> ProductTags { get; set; }
}

另一类: -

 namespace Nop.Core.Domain.Catalog
{
/// <summary>
/// Represents a product
/// </summary>
public partial class Product : BaseEntity, ILocalizedEntity, ISlugSupported
{
    private ICollection<ProductVariant> _productVariants;
    private ICollection<ProductCategory> _productCategories;
    private ICollection<ProductManufacturer> _productManufacturers;
    private ICollection<ProductPicture> _productPictures;
    private ICollection<ProductReview> _productReviews;
    private ICollection<ProductSpecificationAttribute> _productSpecificationAttributes;
    private ICollection<ProductTag> _productTags;

    /// <summary>
    /// Gets or sets the name
    /// </summary>
    public virtual string Name { get; set; }

    /// <summary>
    /// Gets or sets the short description
    /// </summary>
    public virtual string ShortDescription { get; set; }

    /// <summary>
    /// Gets or sets the ShortComment
    /// </summary>
    public string ShortComment { get; set; }



    /// <summary>
    /// Gets or sets the FactSheet
    /// </summary>
    public string FactSheet { get; set; }

    /// <summary>
    /// Gets or sets the HowToReach
    /// </summary>
    public string HowToReach { get; set; }

    /// <summary>
    /// Gets or sets the GeneralInfo
    /// </summary>
    public string GeneralInfo { get; set; }





    /// <summary>
    /// Gets or sets the Itenary ItineraryOption1 
    /// </summary>
    public string ItineraryOption1 { get; set; }



    /// <summary>
    /// Gets or sets the Itenary ItineraryOption2 
    /// </summary>
    public string ItineraryOption2 { get; set; }


    /// <summary>
    /// Gets or sets the Itenary ItineraryOption3
    /// </summary>
    public string ItineraryOption3 { get; set; }


    /// <summary>
    /// Gets or sets the Itenary ItineraryOption4
    /// </summary>
    public string ItineraryOption4 { get; set; }


    /// <summary>
    /// Gets or sets the Itenary ItineraryOption5
    /// </summary>
    public string ItineraryOption5 { get; set; }


    /// <summary>
    /// Gets or sets the Itenary ItineraryOption6
    /// </summary>
    public string ItineraryOption6 { get; set; }

    /// <summary>
    /// Gets or sets the Itenary ItineraryOption7
    /// </summary>
    public string ItineraryOption7 { get; set; }

    /// <summary>
    /// Gets or sets the Itenary ItineraryOption8
    /// </summary>
    public string ItineraryOption8 { get; set; }

    /// <summary>
    /// Gets or sets the Itenary ItineraryOption9
    /// </summary>
    public string ItineraryOption9 { get; set; }

    /// <summary>
    /// Gets or sets the Itenary ItineraryOption10
    /// </summary>
    public string ItineraryOption10 { get; set; }


    /// <summary>
    /// Gets or sets the Itenary ItineraryOption10
    /// </summary>
    public int MealType { get; set; }


    /// <summary>
    /// Gets or sets the Itenary TripType
    /// </summary>
    public int TripType { get; set; }


    /// <summary>
    /// Gets or sets the IsTrip
    /// </summary>
    public bool IsTrip { get; set; }

     /// <summary>
    /// Gets or sets the IsGuranted
    /// </summary>
    public bool IsGuranted { get; set; }


        /// <summary>
    /// Gets or sets the  MinimumBookingforTrip
    /// </summary>
    public int MinimumBookingforTrip { get; set; }


    /// <summary>
    /// Gets or sets the Itenary IsVerified
    /// </summary>
    public bool IsVerified { get; set; }

    public bool IsPrivate { get; set; }

    public bool IsEnabled { get; set; }
    public int VendorId { get; set; }


    /// <summary>
    /// Gets or sets the Itenary IsMoneyBack
    /// </summary>
    public bool  IsMoneyBack { get; set; }


    /// <summary>
    /// Gets or sets the inventory
    /// </summary>
    public string inventory { get; set; }
    /// <summary>
    /// Gets or sets the areasofinterest
    /// </summary>
    public string areasofinterest { get; set; }
    /// <summary>
    /// Gets or sets the VisitorTestimonials
    /// </summary>
    public string VisitorTestimonials { get; set; }

  /// <summary>
    /// Gets or sets the CountryId
    /// </summary>
    public int? CountryId { get; set; }

    /// <summary>
    /// Gets or sets the StateProvinceId
    /// </summary>
    public int? StateProvinceId { get; set; }

    /// <summary>
    /// Gets or sets the City
    /// </summary>
    public int? CityId { get; set; }


    /// <summary>
    /// Gets or sets the LandMark
    /// </summary>
    public int? LandMarkId { get; set; }


    /// <summary>
    /// Gets or sets the full description
    /// </summary>
    public virtual string FullDescription { get; set; }

    /// <summary>
    /// Gets or sets the admin comment
    /// </summary>
    public virtual string AdminComment { get; set; }

    /// <summary>
    /// Gets or sets a value of used product template identifier
    /// </summary>
    public virtual int ProductTemplateId { get; set; }

    /// <summary>
    /// Gets or sets a value indicating whether to show the product on home page
    /// </summary>
    public virtual bool ShowOnHomePage { get; set; }

    /// <summary>
    /// Gets or sets the meta keywords
    /// </summary>
    public virtual string MetaKeywords { get; set; }

    /// <summary>
    /// Gets or sets the meta description
    /// </summary>
    public virtual string MetaDescription { get; set; }

    /// <summary>
    /// Gets or sets the meta title
    /// </summary>
    public virtual string MetaTitle { get; set; }


    /// <summary>
    /// Gets or sets a value indicating whether the product allows customer reviews
    /// </summary>
    public virtual bool AllowCustomerReviews { get; set; }

    /// <summary>
    /// Gets or sets the rating sum (approved reviews)
    /// </summary>
    public virtual decimal ApprovedRatingSum { get; set; }

    /// <summary>
    /// Gets or sets the rating sum (not approved reviews)
    /// </summary>
    public virtual decimal NotApprovedRatingSum { get; set; }

    /// <summary>
    /// Gets or sets the total rating votes (approved reviews)
    /// </summary>
    public virtual int ApprovedTotalReviews { get; set; }

    /// <summary>
    /// Gets or sets the total rating votes (not approved reviews)
    /// </summary>
    public virtual int NotApprovedTotalReviews { get; set; }

    /// <summary>
    /// Gets or sets a value indicating whether the entity is published
    /// </summary>
    public virtual bool Published { get; set; }

    /// <summary>
    /// Gets or sets a value indicating whether the entity has been deleted
    /// </summary>
    public virtual bool Deleted { get; set; }

    /// <summary>
    /// Gets or sets the date and time of product creation
    /// </summary>
    public virtual DateTime CreatedOnUtc { get; set; }

    /// <summary>
    /// Gets or sets the date and time of product update
    /// </summary>
    public virtual DateTime UpdatedOnUtc { get; set; }

    /// <summary>
    /// Promotional message defined by the vendor
    /// </summary>
    public string PromotionalMessage { get; set; }

    /// <summary>
    /// Reward point to be given on every purchase
    /// </summary>
    public int PurchaseRewardPoint { get; set; }

    /// <summary>
    /// maximum reward point that can be consume on this camp
    /// </summary>
    public int MaximumConsumeRewardPoint { get; set; }

    /// <summary>
    /// Minimum reward point needed to purchase a camp
    /// </summary>
    public int MinimumRewardPoint { get; set; }

    /// <summary>
    /// Distance of the camp from source location
    /// </summary>
    public decimal Distance { get; set; }

    /// <summary>
    /// Complete address of a perticular camps
    /// </summary>
    public string CompleteAddress { get; set; }

    /// <summary>
    /// Distance from the source location of the camp
    /// </summary>
    public string DistanceFromSource { get; set; }

    /// <summary>
    /// Gets or sets the product variants
    /// </summary>
    public virtual ICollection<ProductVariant> ProductVariants
    {
        get { return _productVariants ?? (_productVariants = new List<ProductVariant>()); }
        protected set { _productVariants = value; }
    }
    /// <summary>
    /// Gets or sets the collection of ProductCategory
    /// </summary>
    public virtual ICollection<ProductCategory> ProductCategories
    {
        get { return _productCategories ?? (_productCategories = new List<ProductCategory>()); }
        protected set { _productCategories = value; }
    }

    /// <summary>
    /// Gets or sets the collection of ProductManufacturer
    /// </summary>
    public virtual ICollection<ProductManufacturer> ProductManufacturers
    {
        get { return _productManufacturers ?? (_productManufacturers = new List<ProductManufacturer>()); }
        protected set { _productManufacturers = value; }
    }

    /// <summary>
    /// Gets or sets the collection of ProductPicture
    /// </summary>
    public virtual ICollection<ProductPicture> ProductPictures
    {
        get { return _productPictures ?? (_productPictures = new List<ProductPicture>()); }
        protected set { _productPictures = value; }
    }

    /// <summary>
    /// Gets or sets the collection of product reviews
    /// </summary>
    public virtual ICollection<ProductReview> ProductReviews
    {
        get { return _productReviews ?? (_productReviews = new List<ProductReview>()); }
        protected set { _productReviews = value; }
    }

    /// <summary>
    /// Gets or sets the product specification attribute
    /// </summary>
    public virtual ICollection<ProductSpecificationAttribute> ProductSpecificationAttributes
    {
        get { return _productSpecificationAttributes ?? (_productSpecificationAttributes = new List<ProductSpecificationAttribute>()); }
        protected set { _productSpecificationAttributes = value; }
    }

    /// <summary>
    /// Gets or sets the product specification attribute
    /// </summary>
    public virtual ICollection<ProductTag> ProductTags
    {
        get { return _productTags ?? (_productTags = new List<ProductTag>()); }
        protected set { _productTags = value; }
    }

    public List<StateProvince> States { get; set; }
}

}

我的方法是: -

 public static void CopyProperties(dynamic product, dynamic tempProduct)
    {
        PropertyInfo[] productPI = product.GetType().GetProperties();
        PropertyInfo[] tempProductPI = tempProduct.GetType().GetProperties();

        foreach (PropertyInfo temp_prodPI in tempProductPI)
            foreach (PropertyInfo prodPI in productPI)
                if (prodPI.Name == temp_prodPI.Name && temp_prodPI.CanWrite )
                    temp_prodPI.SetValue(tempProduct, prodPI.GetValue(product, null), null);
    }

如何转换这些类型的对象?

0 个答案:

没有答案