[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework

Reflection and operator / retrieval?

Spoon

10/24/2008 6:17:00 PM

So to start what I'm trying to do is use "enhance" the standard format string
options to allow" ,.~" to do the same as ",." without rounding the results.
So maybe there's something really easy I missed.

So...What I'm doing since the formatting is encapsulated way in a report
generation tool I've created a new method that will be called to format
Numbers only. The full function is below.

The problem line is
val = val/a;

Reason being I don't want to modify the actual val type but with it being a
ValueType I also can't just do the division either. Was trying a generic
function but end up with the same problem (new to generics in .NET) and I
tried casting it to a double but that's not really preferable and it didn't
like it anyway.

Anyone have any ideas?

thx


protected static string FormatNumeric(ValueType val, string format)
{
// If we have a null value then return an empty string
if (null == val)
{ return string.Empty; }

// If we have no format string then just return ToString on the
val
if (string.IsNullOrEmpty(format))
{ return val.ToString(); }

// Split into the 3 possible format forms
// posative #s;negative #s;zeros
string[] formats = format.Split(';');

// For comparisons against 0 convert ValueType to a double as it
is the
// most precise of the numerical types so we won't inaccuratly
compare the
// value this way.
System.Reflection.MethodInfo compareMethod =
val.GetType().GetMethod("CompareTo", new Type[] { val.GetType() });
if (null == compareMethod)
{
// throw ex;
}
int rVal = (int)compareMethod.Invoke(val, new object[] { 0 });

int formatIdx = -1;
if (0 < rVal && 1 <= format.Length) // Number is posative use
format 1
{ formatIdx = 0; }
else if (0 > rVal && 2 <= format.Length) // Number is negative
use format 2
{ formatIdx = 1; }
else if (0 == rVal && 3 <= format.Length) // Number is equal to
0 use format 3
{ formatIdx = 2; }

// If we have a posative value
if (-1 < formatIdx)
{
// Simple regex expression. Any # of ,'s followed by a
period(.) and then a tilda(~)
string reg = ",+\\.~";
Regex regx = new Regex(reg);
Match match = regx.Match(formats[formatIdx]);
if (match.Success)
{
// Now figure out how many ,'s exist before the . and
adjust
// the 1000's divide value accordingly. (each comma
represents a thousnds power)
int idx = match.Value.IndexOf('.') + 1;
long a = (int)System.Math.Pow(1000, idx);

System.Reflection.MethodInfo divide =
val.GetType().GetMethod("operator /", new Type[] { typeof(int) });
val = val/a;
format = Regex.Replace(format, reg, string.Empty);
}
}

System.Reflection.MethodInfo tostringMethod =
val.GetType().GetMethod("ToString", new Type[] {typeof(string)});
if (null == tostringMethod)
{
// throw ex;
}
return (string)tostringMethod.Invoke(val, new object[] { format
});
}

1 Answer

Marc Gravell

10/27/2008 9:30:00 AM

0

Accessing operators at runtime in .NET is a pain; some types have static
op_{blah} methods; some (such as primatives: int etc) don't [the
compiler deals with it directly]. You also might need to worry about
"lifted operators" for nullable types. Basically, a real pain.

However; if you are using .NET 3.5, I have a generic operator
implementation that works for any type:

www.pobox.com/~skeet/csharp/miscutil/usage/genericoperators.html

The Operator class is part of MiscUtil:

www.pobox.com/~skeet/csharp/miscutil/

Marc
[C# MVP]