/// <summary> /// Creates measurement object instance from default raw value constructor (one parameter of type double) /// </summary> /// <param name="getter">Getter method to supply raw value</param> /// <returns>Created object instance</returns> /// <exception cref="InvalidOperationException">If constructor was not found</exception> protected static TS Create(DoubleGetter getter) { var constructorInfo = typeof(TS).GetConstructor(new[] { typeof(double) }); if (constructorInfo == null) { throw new InvalidOperationException("Unable to find raw value constructor (one parameter of type double)"); } return((TS)constructorInfo.Invoke(new object[] { getter.Invoke() })); }
public void Delegate_CreateDelegate_InternalMethod() { PrivateType pt = new PrivateType(1.0, 2.0); MethodInfo mi = typeof(PrivateType).GetMethod("Add", BindingFlags.NonPublic | BindingFlags.Instance); Assert.IsNotNull(mi, "Add"); DoubleGetter dget = (DoubleGetter)Delegate.CreateDelegate(typeof(DoubleGetter), pt, mi); Assert.IsNotNull(dget, "Delegate.CreateDelegate(Type,object,string)"); Assert.AreEqual(3.0, dget(), "dget-1"); dget = (DoubleGetter)Delegate.CreateDelegate(typeof(DoubleGetter), pt, "Add"); Assert.IsNotNull(dget, "Delegate.CreateDelegate(Type,object,string)"); Assert.AreEqual(3.0, dget(), "dget-2"); }