{"record":{"id":"8172943ff4ebf422","repo":"dotnet/machinelearning","slug":"getter-must-be-of-type-typeof-valuegetter-tvalue","errorCode":null,"errorMessage":"getter must be of type '{typeof(ValueGetter<TValue>).FullName}'","messagePattern":"getter must be of type '(.+?)'","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.ML.DataView/DataViewSchema.cs","lineNumber":369,"sourceCode":"                /// Returns a <see cref=\"Annotations\"/> row that contains the current contents of this <see cref=\"Builder\"/>.\n                /// </summary>\n                public Annotations ToAnnotations()\n                {\n                    var builder = new DataViewSchema.Builder();\n                    foreach (var item in _items)\n                        builder.AddColumn(item.Name, item.Type, item.Annotations);\n                    return new Annotations(builder.ToSchema(), _items.Select(x => x.Getter).ToArray());\n                }\n\n                private void AddDelegate<TValue>(string name, DataViewType type, Delegate getter, Annotations annotations)\n                {\n                    Debug.Assert(!string.IsNullOrEmpty(name));\n                    Debug.Assert(type != null);\n                    Debug.Assert(getter != null);\n\n                    var typedGetter = getter as ValueGetter<TValue>;\n                    if (typedGetter == null)\n                        throw new ArgumentException($\"{nameof(getter)} must be of type '{typeof(ValueGetter<TValue>).FullName}'\", nameof(getter));\n                    _items.Add((name, type, typedGetter, annotations));\n                }\n            }\n        }\n\n        /// <summary>\n        /// Class containing operations to build a <see cref=\"DataViewSchema\"/>.\n        /// </summary>\n        public sealed class Builder\n        {\n            private readonly List<(string Name, DataViewType Type, Annotations Annotations)> _items;\n\n            /// <summary>\n            /// Create a new instance of <see cref=\"Builder\"/>.\n            /// </summary>\n            public Builder()\n            {\n                _items = new List<(string Name, DataViewType Type, Annotations Annotations)>();","sourceCodeStart":351,"sourceCodeEnd":387,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.ML.DataView/DataViewSchema.cs#L351-L387","documentation":"Builder.AddDelegate casts the getter delegate to ValueGetter<TValue> and throws ArgumentException(\"getter must be of type 'ValueGetter<TValue>'\") when the cast fails. The builder requires a typed ValueGetter<TValue> delegate to store values into the schema column; any other delegate signature cannot be used. Note the Debug.Asserts indicate this method is internal/contractually pre-validated, so this mostly surfaces from dynamic invocation.","triggerScenarios":"Passing a lambda or method group convertible to Func<TValue> but not to ValueGetter<TValue> (whose signature is void(ref TValue)), e.g. AddDelegate(name, type, (out int v) => ...) or a Delegate obtained via reflection/Delegate.CreateDelegate with the wrong signature.","commonSituations":"Reflection-based schema construction where the delegate was created against the wrong generic type argument; wrapping existing getters with incompatible anonymous delegates; generic helpers passing Delegate objects.","solutions":["Supply a ValueGetter<TValue> delegate: (ref TValue dst) => dst = value, not Func/Action with different signatures.","Ensure the generic type argument matches the getter's type argument (ValueGetter<int> for TValue=int).","When using reflection, call Delegate.CreateDelegate(typeof(ValueGetter<T>), target, method) with the correct T.","Cast defensively: if (getter is ValueGetter<TValue> g) builder.AddDelegate(...) else throw a clear error."],"exampleFix":"// before\nbuilder.AddDelegate(\"Score\", NumberDataViewType.Single, (float) => GetScore());\n// after\nfloat GetScore(ref float dst) { dst = ComputeScore(); }\nbuilder.AddDelegate(\"Score\", NumberDataViewType.Single, (ValueGetter<float>)GetScore);","handlingStrategy":"type-guard","validationCode":"if (!(getter is ValueGetter<TValue>)) throw new ArgumentException(\"getter must be a ValueGetter<TValue>\");","typeGuard":"bool IsTypedGetter<TV>(Delegate d) => d is ValueGetter<TV>;","tryCatchPattern":"try { builder.AddDelegate(name, type, getter); } catch (ArgumentException) { /* rebuild delegate as ValueGetter<TValue> */ }","preventionTips":["Always write getters with the void(ref TValue) signature","Use explicit (ValueGetter<TValue>) casts so the compiler catches mismatches","Create reflection delegates with Delegate.CreateDelegate(typeof(ValueGetter<T>), ...)"],"tags":["csharp","delegate-signature","type-mismatch","ml-net"],"backgroundTag":"invalid-argument-value","analyzedSha":"7b76e69cf964daeca3f1377af6bc5543284d56c6","analyzedAt":"2026-09-11T12:35:38.930Z","contentChangedAt":"2026-09-11T12:35:38.930Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}