using System;
using System.Collections.Generic;
using LoreSoft.MathExpressions.Properties;
using System.Globalization;
using System.Runtime.Serialization;
using System.Security.Permissions;
namespace LoreSoft.MathExpressions
{
///
/// Class representing a collection of variable names and values.
///
///
/// Variable names can only contain letters, numbers and symbols are not allowed.
///
[Serializable]
public class VariableDictionary : Dictionary
{
private MathEvaluator _evaluator;
/// Initializes a new instance of the class.
/// The evaluator.
internal VariableDictionary(MathEvaluator evaluator)
: base(StringComparer.OrdinalIgnoreCase)
{
_evaluator = evaluator;
base.Add(MathEvaluator.AnswerVariable, 0);
base.Add("pi", Math.PI);
base.Add("e", Math.E);
}
///
/// Initializes a new instance of the class.
///
/// A object containing the information required to serialize the .
/// A structure containing the source and destination of the serialized stream associated with the .
protected VariableDictionary(SerializationInfo info, StreamingContext context)
: base(info, context)
{ }
/// Adds the specified variable and value to the dictionary.
/// The name of the variable to add.
/// The value of the variable.
/// When variable name is null.
/// When variable name contains non-letters or the name exists in the list.
///
///
///
public new void Add(string name, double value)
{
Validate(name);
base.Add(name, value);
}
private void Validate(string name)
{
if (string.IsNullOrEmpty(name))
throw new ArgumentNullException("name");
if (_evaluator.IsFunction(name))
throw new ArgumentException(
string.Format(CultureInfo.CurrentCulture,
Resources.VariableNameConflict, name), "name");
for (int i = 0; i < name.Length; i++)
if (!char.IsLetter(name[i]))
throw new ArgumentException(Resources.VariableNameContainsLetters, "name");
}
///
/// Implements the interface and returns the data needed to serialize the instance.
///
/// A object that contains the information required to serialize the instance.
/// A structure that contains the source and destination of the serialized stream associated with the instance.
[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
}
}
}