开发者

How to set Type.IsGenericTypeDefinition to true?

开发者 https://www.devze.com 2023-03-08 13:22 出处:网络
I have an many class with BeginDate and EndDate properties, I want to build generic method that return one object as List of the same type but with BeginDate=EndDate.

I have an many class with BeginDate and EndDate properties, I want to build generic method that return one object as List of the same type but with BeginDate=EndDate.

Ex:

Item.BeginDate = 2011-05-01 , Item.EndDate = 2011-05-03

the result must be

Item1.BeginDate = 2011-05-01 , Item1.EndDate = 2011-05-01

Item2.BeginDate = 2011-05-02 , Item2.EndDate = 2011-05-02

Item3.BeginDate = 2011-05-03 , Item3.EndDate = 2011-05-03

I tried the following code

<Extension()> _
        Public Function GetRatesPerDay(Of T As Class)(ByVal oldRatesList As List(Of T)) As List(Of T)
            If oldRatesList Is Nothing OrElse oldRatesList.Count = 0 Then
                Return New List(Of T)
            End If
            Dim ratesIn开发者_开发百科Days As New List(Of T)

            Dim oldBeginDate = oldRatesList.Where(Function(D) D IsNot Nothing).Min(Function(D) GetType(T).GetProperty("BeginDate", GetType(DateTime)).GetValue(D, Nothing))
            Dim oldEndDate = oldRatesList.Where(Function(D) D IsNot Nothing).Max(Function(D) GetType(T).GetProperty("EndDate", GetType(DateTime)).GetValue(D, Nothing))

            Dim currentDay As DateTime = oldBeginDate

            Dim typeArgs() As Type = {GetType(DateTime), GetType(DateTime), GetType(Nullable(Of Double)), GetType(String), GetType(HUMPSBaseClasses.RoomRateType)}
            Dim constructed As Type = GetType(T).MakeGenericType(typeArgs)

            While currentDay <= oldEndDate
                Dim dayRate = (From D In oldRatesList _
                                Where D IsNot Nothing _
                                AndAlso currentDay >= GetType(T).GetProperty("BeginDate", GetType(DateTime)).GetValue(D, Nothing) _
                                AndAlso currentDay <= GetType(T).GetProperty("EndDate", GetType(DateTime)).GetValue(D, Nothing) _
                                Select New With {.NightRate = GetType(T).GetProperty("NightRate", GetType(Nullable(Of Double))).GetValue(D, Nothing), _
                                                 .RatePlanID = GetType(T).GetProperty("RatePlanID", GetType(System.String)).GetValue(D, Nothing), _
                                                 .RoomRateType = GetType(T).GetProperty("RoomRateType", GetType(HUMPSBaseClasses.RoomRateType)).GetValue(D, Nothing) _
                                                 }).SingleOrDefault()

                If dayRate IsNot Nothing Then
                    Dim tempRate As Object = Activator.CreateInstance(constructed)
                    tempRate = New With {.BeginDate = currentDay, _
                                             .EndDate = currentDay, _
                                             .NightRate = dayRate.NightRate, _
                                             .RatePlanID = dayRate.RatePlanID, _
                                             .RoomRateType = dayRate.RoomRateType}
                    ratesInDays.Add(tempRate)
                End If
                currentDay = currentDay.AddDays(1)
            End While
            Return ratesInDays
        End Function

but I faced a problem that Type.IsGenericTypeDefinition of my classes is false.

How can I set it to true?


You can't. That is a read-only property.

You're getting false because the class isn't generic. Only your method is.

You should be able to reflect on the type and retrieve your MethodInfo. You can then use either MethodInfo.IsGenericMethod or MethodInfo.IsGenericMethodDefinition.

Your other option would be to modify the class to be generic, but that would be overkill since this is (I'm guessing here) the only method that needs the generic parameter.


You can't set the value of this property - it is read-only.

0

精彩评论

暂无评论...
验证码 换一张
取 消