[Mono-dev] [PATCH] Extract mono_exception_get_message_string from mono_print_exception

Robert Jordan robertj at gmx.net
Fri Jul 2 15:43:27 EDT 2010


Hi Michael,

On 02.07.2010 21:13, Michael Hutchinson wrote:
>
> Implemented - new patch attached.

> +MonoString *
> +mono_object_to_string (MonoObject *obj, MonoObject **exc)
> +{
> +	MonoString *str;
> +	MonoMethod *method;
> +	MonoClass *klass;
> +	
> +	klass = obj->vtable->klass;
> +	method = NULL;
> +	while (klass && method == NULL) {
> +		method = mono_class_get_method_from_name_flags (klass, "ToString", 0, METHOD_ATTRIBUTE_VIRTUAL | METHOD_ATTRIBUTE_PUBLIC);
> +		if (method == NULL)
> +			klass = klass->parent;
> +	}
> +
> +	g_assert (method);
> +
> +	return (MonoString *) mono_runtime_invoke (method, obj, NULL, exc);
> +}

You can get rid of the loop and optimize it further by caching
System.Object's ToString method:

MonoString *
mono_object_to_string (MonoObject *obj, MonoObject **exc)
{
	static MonoMethod *to_string = NULL;
	MonoMethod *method;

	g_assert (obj);

	if (!to_string)
		to_string = mono_class_get_method_from_name_flags 
(mono_get_object_class (), "ToString", 0, METHOD_ATTRIBUTE_VIRTUAL | 
METHOD_ATTRIBUTE_PUBLIC);

	method = mono_object_get_virtual_method (obj, method);

	return (MonoString *) mono_runtime_invoke (method, obj, NULL, exc);
}

Robert



More information about the Mono-devel-list mailing list